Look up hostname from Maven

前端 未结 5 1986
無奈伤痛
無奈伤痛 2020-12-09 03:42

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:

...

   

        
相关标签:
5条回答
  • 2020-12-09 04:00

    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>
    
    0 讨论(0)
  • 2020-12-09 04:11

    The comment posted by user1885834 was working best for me: Create profiles for Windows and Linux and use the respective environment variables to define a new property ${hostname}, to be used anywhere.

    <profile>
       <id>unix</id>
       <activation>
          <os>
             <family>unix</family>
          </os>
       </activation>
       <properties>
          <hostname>${env.HOSTNAME}</hostname>
       </properties>
    </profile>
    <profile>
       <id>windows</id>
       <activation>
          <os>
             <family>Windows</family>
          </os>
       </activation>
       <properties>
          <hostname>${env.COMPUTERNAME}</hostname>
       </properties>
    </profile>
    
    0 讨论(0)
  • 2020-12-09 04:16

    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>
    
    0 讨论(0)
  • 2020-12-09 04:21

    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>
    
    0 讨论(0)
  • 2020-12-09 04:22

    ${env.COMPUTERNAME} works for me..

    0 讨论(0)
提交回复
热议问题