サーバーのセットアップ スクリプトとして Ant を使用しており、サーバーの完全修飾名を取得する必要があります。Ant を使用してどのように取得できますか、または Ant で可能ですか?
完全修飾ホスト名は次のようになります: xxx.company.com
サーバーのセットアップ スクリプトとして Ant を使用しており、サーバーの完全修飾名を取得する必要があります。Ant を使用してどのように取得できますか、または Ant で可能ですか?
完全修飾ホスト名は次のようになります: xxx.company.com
<exec executable="hostname" outputproperty="computer.hostname"/>
それ以外の場合は、Marc O'Connor の groovy ソリューションを使用し
てくださいnslookup ServerName
<groovy>
properties.'hostname' = "hostname".execute().text
//or
properties.'hostname' = InetAddress.getLocalHost().getHostName()
properties.'hostnamefull' = "nslookup ${"hostname".execute().text}".execute().text.find(/Name:\s(.+)/).split(/:\s+/)[1]
</groovy>
<echo>
$${hostname} => ${hostname}
$${hostnamefull} => ${hostnamefull}
</echo>
HostInfo
現在のマシンのホスト名とドメインを含むプロパティを設定するために使用できるAnt タスクがあります。
または、Linux/Unix で実行している場合は、次のhostname
コマンドを呼び出すことができます。
<exec executable="hostname" outputproperty="myhostname">
<arg line="-f"/>
</exec>
その後、完全修飾ホスト名が で使用可能になり${myhostname}
ます。
編集:完全にプラットフォームに依存しないソリューションの場合、次のような(テストされていない)カスタムタスクが機能するはずです:
public class GetHost extends Task
{
private String property;
public void setProperty(String property)
{
this.property = property;
}
@Override
public void execute() throws BuildException
{
if (property == null || property.length() == 0)
{
throw new BuildException("Property name must be specified.");
}
else
{
try
{
String hostName = InetAddress.getLocalHost().getHostName();
getProject().setProperty(property, hostName);
}
catch (IOException ex)
{
throw new BuildException(ex);
}
}
}
}
これは次のように使用できます。
<GetHost property="myhostname" />
Mavenに対して行った回答の再ハッシュ:-)
組み込みの groovy スクリプトを使用して、Java ホスト名のルックアップを実行します。
<groovy>
properties["hostname"] = InetAddress.getLocalHost().getHostName()
</groovy>
プロジェクトは自己文書化されています:
$ ant -p
Buildfile: /home/mark/tmp/build.xml
This is a demo project answering the followng stackoverflow question:
https://stackoverflow.com/questions/14653733
First install 3rd party dependencies
ant bootstrap
Then run the build
ant
Expect the following output
print-hostname:
[echo] Hostname: ?????
<project name="demo" default="print-hostname">
<description>
This is a demo project answering the followng stackoverflow question:
https://stackoverflow.com/questions/14653733
First install 3rd party dependencies
ant bootstrap
Then run the build
ant
Expect the following output
print-hostname:
[echo] Hostname: ?????
</description>
<target name="bootstrap" description="Install 3rd party dependencies">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0/groovy-all-2.1.0.jar"/>
</target>
<target name="print-hostname" description="Retrieve and print the hostname">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
properties["hostname"] = InetAddress.getLocalHost().getHostName()
</groovy>
<echo message="Hostname: ${hostname}"/>
</target>
</project>
もう 1 つの方法は、この情報をプロパティに設定する JavaScript スクリプト定義を作成することです。
<scriptdef name="get-hostame" language="javascript">
<attribute name="prefix" /> <![CDATA[
importClass(java.net.InetAddress);
address = InetAddress.getLocalHost();
prefix = attributes.get("prefix");
project.setNewProperty(prefix + ".hostname", address.getHostName());
project.setNewProperty(prefix + ".fqdn", address.getCanonicalHostName());
project.setNewProperty(prefix + ".address", address.getHostAddress());
]]>
</scriptdef>
次のように呼び出すことができます。
<get-hostame prefix="uwi.host" />
結果は次のとおりです。
[echoproperties] uwi.host.address=10.666.666.666
[echoproperties] uwi.host.fqdn=myhost.stackoverflow.com
[echoproperties] uwi.host.hostname=myhos
これは、私がここで見つけたいくつかのアドバイスに基づいています: http://grokbase.com/t/ant/user/051sygfj9b/any-way-to-get-at-the-machine-name