Look up hostname from Maven

浪尽此生 提交于 2019-12-17 18:34: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:

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

Any suggestions?


回答1:


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>



回答2:


${env.COMPUTERNAME} works for me..




回答3:


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>



回答4:


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>


来源:https://stackoverflow.com/questions/7441265/look-up-hostname-from-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!