Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven-properties-filtering-like for Java annotations?

Currently, I know how to do this kind of filtering using Maven:

pom.xml

<properties>
    <foo>bar</foo>
</properties>

app.properties

foo=${foo}

But is it possible to do that kind of filtering, using Maven, Spring or any tool else?

MyClass.java

@MyAnnotation("${foo}") // ${foo} should get replaced at compile time
public void getData() {
    return data;
}
like image 588
sp00m Avatar asked Jan 09 '13 16:01

sp00m


People also ask

What are the properties supported by Maven?

Also, the maven supports a large scale of built-in properties. Here over this article, we will discuss all these kind of properties supported by maven. What are the types of properties in Maven? Maven supports a great set of property references. In pom.xml it can get the os configured properties, Java properties.

What is @WebFilter annotation in Java?

This annotation is processed by the container at deployment time, and the corresponding filter applied to the specified URL patterns, servlets, and dispatcher types. The classes annotated with @WebFilter must implement javax.servlet.Filter.

Why are my properties files not encoding in Maven?

So unless you have configured the encoding parameter in Maven Resources Plugin explicitly this is what you get. When the Properties class is used to read and write properties files they require that the properties files use ISO-8859-1 encoding. This is still the case for Java 11, as can be seen in the API documentation for the Properties class.

What are built-in annotations in Java?

Java defines seven built-in annotations. Four are imported from java.lang.annotation: @Retention, @Documented, @Target, and @Inherited. It is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form.


1 Answers

Have you tried using an execution of the resource plugin. You can point it at your Java source and use its normal filtering, as far as I know.

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

  <properties>
    <my.name>chad</my.name>
    <java.property>//comment</java.property>
  </properties>

  <build>
    <sourceDirectory>target/processed-source/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>

         <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
            <targetPath>../processed-source/java</targetPath>
        </resource>
    </resources>

  </build>

So, the first thing is that you route the processed java source into a special folder in the target directory. Next, you have to reconfigure the compiler plugin to NOT compile the unfiltered source, and, instead, compile the new source. Note, as with all things maven, you can configure a lot more than this.

This blog entry is useful.

like image 172
chad Avatar answered Oct 10 '22 05:10

chad