Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Look up hostname from Maven

Tags:

I'm looking for a way to look up hostname and set it as a property in Maven.

This does not work in all environments:

...
<properties>
   <hostname>${env.HOSTNAME}</hostname>
</properties>
...

Any suggestions?

like image 294
Stein Inge Morisbak Avatar asked Sep 16 '11 07:09

Stein Inge Morisbak


4 Answers

Use a groovy script to set the project property

    <plugin>
        <groupId>org.codehaus.groovy.maven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <executions>
            <execution>
                <phase>generate-resources</phase>
                <goals>
                    <goal>execute</goal>
              </goals>
               <configuration>
                  <source>
                  project.properties["hostname"] = InetAddress.getLocalHost().getHostName()
                 </source>
             </configuration>
         </execution>
      </executions>
 </plugin>
like image 120
Mark O'Connor Avatar answered Sep 19 '22 13:09

Mark O'Connor


${env.COMPUTERNAME} works for me..

like image 31
user3120592 Avatar answered Sep 22 '22 13:09

user3120592


The Maven Build Helper plugin 3.1.0 was just released with goals for getting the hostname, ip address, port reserving etc (http://www.mojohaus.org/build-helper-maven-plugin/usage.html).

Add this to build plugins:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>init-build-properties</id>
            <goals>
                <goal>hostname</goal>
            </goals>
            <configuration>
                <hostnameProperty>my.hostname</hostnameProperty>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 3
Maddin Avatar answered Sep 18 '22 13:09

Maddin


I ended up with a simple solution to the cross-plattform problem:

<manifestEntries>  
    <Build-Host-Linux>${env.HOSTNAME}</Build-Host-Linux>
    <Build-Host-Windows>${env.COMPUTERNAME}</Build-Host-Windows>
</manifestEntries>
like image 2
AxelW Avatar answered Sep 21 '22 13:09

AxelW