开发者

How can I add a calculated property to a Maven project using a plugin?

I have written a Maven plugin to grab the machine IP address and would like to be able to create a property so that the IP address gets filtered into a file (via ${ipaddress}) when archetype generation happens.

I have not been able to find how 开发者_JAVA技巧to do this. Does anyone know?


You can use org.codehaus.groovy.maven plugin to get IP and set it to props. In my example I've set retrieved IP to property localIP and it's available on next stages as any other maven props, i.e. as ${localIP}.

                 <plugin>
                    <groupId>org.codehaus.groovy.maven</groupId>
                    <artifactId>gmaven-plugin</artifactId>
                    <version>1.0</version>
                    <executions>
                      <execution>
                        <id>get-local-ip</id>
                        <phase>initialize</phase>
                        <goals>
                          <goal>execute</goal>
                        </goals>
                        <configuration>
                          <classpath>
                            <element>
                              <groupId>commons-lang</groupId>
                              <artifactId>commons-lang</artifactId>
                              <version>2.4</version>
                             </element>
                          </classpath>
                          <source>
                              java.net.InetAddress address=InetAddress.getByName("${env.COMPUTERNAME}");
                              project.properties.localIP=address.getHostAddress();
                          </source>
                        </configuration>
                      </execution>
                    </executions>
                </plugin>


The properties-maven-plugin reads properties from a file and makes them available to a build as if they have been defined inline.

You can either have your plugin output the ip to a file then use the properties plugin to read it, or pinch the source from the properties plugin to set the property in your own plugin.

Essentially you just get the Properties from the MavenProject and add your own entry/ies.


Well the problem is - your properties are processed before you run the build and cannot be overwritten during the build. At least that is my experience I would love to be proved wrong and even accept negative points for that :) So one possibility - create script that runs your program, populates system property and then runs mvn build right after (I know it's ugly)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜