views:

142

answers:

3

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 to do this. Does anyone know?

A: 

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 on possibility - create script that runs your program, populates system property and then runs mvn build right after (I know it's ugly)

DroidIn.net
Aren't there other plugins that do this, though? Like the BuildNumber plugin? I tried looking at that one, but it's a lot more involved than just setting a property. I don't know what's really available to the plugin.
I Never Finish Anythi
Could be, could be... I had an issue where I was trying to trigger profile execution based on property defined both in parent and the child module and I found out that once set - the property never changes throughout the build
DroidIn.net
+1  A: 

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.

Rich Seller
The plugin does just that... grabs the project properties and adds on your ip address. The problem I'm having is that I can't actually get the plugin to run when you execute "mvn archetype:generate". This is when I would like it to happen so that the generated project will have the value already in it.
I Never Finish Anythi
A: 

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>
tbat