¿Hay alguna manera de usar la propiedad Maven en la clase Java durante la compilación?

I just want to use maven placeholder in my Java class at compile time in order to reduce duplication.

Algo como eso:

pom.xml

<properties>
  <some.version>1.0</some.version>
</properties>

SomeVersion.java

package some.company;

public class SomeVersion {

    public static String getVersion() {
        return "${some.version}"
    }

}

preguntado el 31 de julio de 12 a las 13:07

4 Respuestas

simply create file app.properties in src/main/resources with content like this

application.version=${project.version}

then enable maven filtering like this

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

That's all - in app code just read properties file

ClassPathResource resource = new ClassPathResource( "app.properties" );
p = new Properties();
InputStream inputStream = null;
try {
    inputStream = resource.getInputStream();
    p.load( inputStream );
} catch ( IOException e ) {
    LOGGER.error( e.getMessage(), e );
} finally {
    Closeables.closeQuietly( inputStream );
}

and provide method like this

public static String projectVersion() {
    return p.getProperty( "application.version" );
}

contestado el 03 de mayo de 17 a las 15:05

+1 - this is definitely better than feeding the source code through a preprocessor. - Stephen C

No es ClassPathResource spring-specific? OP did not include the spring etiqueta. - fegemo

This approach, in my own view, is better than the uses of propiedades-maven-plugin (It works too) described aquí - aspadacio

The property in my case is returning value as ${project.version} Also I have to files in my resource folder but I am reading only one of them in which is i have written version=${project.version} - Nitish Kumar

Just for the heads up ClassPathResource works with spring only. If you want to implement this logic in plain java it won't work - Nitish Kumar

The simplest way I know of doing that is to use Complemento de plantillas de Maven.

Add plugin declaration to your pom:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>templating-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <id>filter-src</id>
            <goals>
                <goal>filter-sources</goal><!-- add this if you filter main sources -->
                <goal>filter-test-sources</goal><!-- add this if you filter test sources -->
            </goals>
        </execution>
    </executions>
</plugin>

If you're filtering main sources:

  • crear una carpeta src/main/java-templates
  • Move the source files you want to be filtered to that folder. Reproduce package tree structure, as if you were in src/main.

If you're filtering tests sources too:

  • crear una carpeta src/test/java-templates
  • Move the source files you want to be filtered to that folder. Reproduce package tree structure, as if you were in src/test.

Assuming that your sources contain valid placeholders like:

package some.company;

public class SomeVersion {

    public static String getVersion() {
        return "${project.version}"
    }

}

Ahora cuando tu compile or test your project, those placeholders should be already valued.

Espero eso ayude.

Respondido 21 ago 17, 10:08

If you are working with Spring, you can inject a property. The steps are:

  1. Inside POM file you define all profiles needed and each profile must have your custom property, in your case

<profile>
	<id>dev</id>
	<properties>
		<some.version>Dev Value</some.version>
	</properties>
</profile>

  1. In the section build of your profile, you define the filtering injection.
  2. Under the your project resources directory, you create a properties file (any mnemonic christian name) and put your prop to be injected:

custom.some.version=${some.version}

  1. On the spring-context file you define the properties placeholder and define your bean or beanProperty:

<context:property-placeholder location="classpath*:/META-INF/*.properties"/>
...
<bean id="customConfig" class="com.brand.CustomConfig">
	<property name="someVersion" value="${custom.some.version}" />
</bean>
...

  1. Create your class.
package com.brand;

public class CustomConfig {
  private String someVersion;

  public getSomeVersion() {
  return this.someVersion;
  }

  public setSomeVersion(String someVersion) {
  this.someVersion = someVersion;
  }
}
  1. Inject where you want to use. This example is with autowired bean, but you can use and autowired property too.
package com.brand.sub

public class YourLogicClass {
  @Autowired
  private CustomConfig customConfig;

  // ... your code
}

On the final compilation, you have the correct values.

respondido 04 nov., 15:20

Even though it's not a very nice solution it is possible with the default maven resource plugin.

First you need to specify the resource plugin.

<project>
  <build>
    <!-- Configure the source files as resources to be filtered
      into a custom target directory -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <filtering>true</filtering>
        <targetPath>../filtered-sources/java</targetPath>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>

Afterwards you will need to change the 'default' configuration of the compiler plugin.

<project>
  <build>
      <!-- Overrule the default pom source directory to match
            our generated sources so the compiler will pick them up -->
      <sourceDirectory>target/filtered-sources/java</sourceDirectory>
  </build>
</project> 

Respondido 31 Jul 12, 14:07

I agree it's not the best or perhaps the most proper way of doing it, but it is a forma de hacerlo. - Jeroen

@Jeroen yes, I agree with you. I can't reproduce that way, but it makes my knowledge wider. Thanks for the answer. - Dmitro Chyzhykov

To do this, use the templating-maven-plugin. It does some of the things included in the maven-resources-plugin, but without the burden of writing tens of XML lines. See mojo.codehaus.org/templating-maven-plugin and my answer on another SO question: stackoverflow.com/a/18452939/345845 - Bautista Mathus

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.