Is there a way to use maven property in Java class

2019-01-13 13:03发布

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

Something like that:

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}"
    }

}

4条回答
看我几分像从前
2楼-- · 2019-01-13 13:32

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.

查看更多
在下西门庆
3楼-- · 2019-01-13 13:33

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> 
查看更多
唯我独甜
4楼-- · 2019-01-13 13:42

The simplest way I know of doing that is to use Templating Maven Plugin.

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:

  • Create folder 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:

  • Create folder 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}"
    }

}

Now when you compile or test your project, those placeholders should be already valued.

Hope it helps.

查看更多
冷血范
5楼-- · 2019-01-13 13:47

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" );
}
查看更多
登录 后发表回答