通过maven更改静态字段值

时间:2017-10-27 09:54:10

标签: java maven

我有一个这样的课程:

public final class MyClass {

    private static String VERSION = "1.0.0";
}

是否有一种简单的方法可以更改 VERSION 字段值,而不是 1.0.0 maven项目版本值( $ {project.version} ),例如2.0.0-SNAPSHOT?

P.S。例如,可以在 generate-sources 阶段完成。

1 个答案:

答案 0 :(得分:3)

此案例的最佳选择是使用完全解决此问题的templating-maven-plugin

所以这意味着只需将一个类模板放入包含正确包裹位置的src/main/java-templates中,然后放入${project.version}等占位符。 在构建期间,这些类将被正确过滤并自动构建到生成的jar中......

<plugins>
  <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>
        </goals>
            <configuration>              
              <!-- 
                Note the two following parameters are the default one. 
                These are specified here just as a reminder. 
                But as the Maven philosophy is strongly about conventions, 
                it's better to just not specify them.
              -->
              <sourceDirectory>${basedir}/src/main/java-templates</sourceDirectory>
              <outputDirectory>${project.build.directory}/generated-sources/java-templates</outputDirectory>
            </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>