Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

java - In Maven, how can I dynamically build a property value at runtime?

In maven it is very easy to set properties in a pom with the following syntax:

...
<properties>
  <myValue>4.06.17.6</myValue>
 </properties>
...

Now I need to build a property which depends on the version of my pom. For creating the property i want to do the following (java pseudo code):

String[] parts = version.split("\.");
String.format("V%s_%s_%s_P%s", splitted[0],  splitted[1],splitted[2],splitted[3]);
// example: 4.06.17.6 => V_4_06_17_P6

It should be dynamic, because it is used as a tag name in our repository and must always be in sync with the version of the artifact.

Any ideas how to create that "dynamic" properties?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Mojo's Build-Helper Maven Plugin can help you out here.

There are a number of goals that can be used to help transform properties.

There is

Probably regex-property is the one you want, but if your version numbers conform to the "standards" the other two might save you.

To use the regex-property goal you would do something like

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>regex-property</id>
            <goals>
              <goal>regex-property</goal>
            </goals>
            <configuration>
              <name>tag.version</name>
              <value>${project.version}</value>
              <regex>^([0-9]+).([0-9]+).([0-9]+).([0-9]+).(-SNAPSHOT)?$</regex>
              <replacement>V$1_$2_$3_P$4</replacement>
              <failIfNoMatch>true</failIfNoMatch>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Note: my regex might be slightly off so you should test the above.

Note: The property value will only be available for executions after the phase that this execution is bound to. The default phase that it is bound to is validate but if you are on a different lifecycle (e.g. the site lifecycle) the value will not be available.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...