sitelink1 | |
---|---|
sitelink2 | |
sitelink3 | http://1 |
- 요약 -
1. 실행 명령어 : ant -Dprop1="My Property" -Dprop2="???" run
2. XML문서 상단에 <property> 를 정의하면 <sysproperty> 를 이용하여 실행시에 parameter 를 이용할 수 있다
- 본문 -
3.6.1 Problem
You want to pass system properties to a buildfile. Java system properties are a more portable alternative to environment variables.
3.6.2 Solution
Pass the system properties to Ant using the -D command-line argument. For example:
ant -Dprop1="My Property" runWithin the buildfile, refer to the property using Ant's ${prop1} syntax. You can specify default values for properties using the <property> tag, and you can pass system properties to Java applications using the<sysproperty> tag nested within the <java> element.
3.6.3 Discussion
Example 3-3 shows an Ant buildfile that demonstrates system properties. It echoes the property name/value pairs to the console, and then invokes a Java application that echoes the same properties.
Example 3-3. Buildfile demonstrating system properties
<?xml version="1.0"?> <project name="sysprops" default="run" basedir="."> <!-- define two properties --> <property name="prop1" value="Property 1 from Buildfile"/> <property name="prop2" value="Property 2 from Buildfile"/> <target name="clean"> <delete dir="com"/> </target> <target name="compile"> <javac srcdir="." destdir="."> <classpath path="."/> </javac> </target> <target name="run" depends="compile"> <!-- echo each of the properties to the console --> <echo message="Now in buildfile..."/> <echo message="prop1 = ${prop1}"/> <echo message="prop2 = ${prop2}"/> <!-- The 'prop3' property must be defined on the command line or it shows up like '${prop3}' --> <echo message="prop3 = ${prop3}"/> <echo message="user.home = ${user.home}"/> <!-- execute the main( ) method in a Java class --> <java classname="com.oreilly.javaxp.ShowProps"> <classpath path="."/> <!-- pass one of the properties --> <sysproperty key="prop1" value="${prop1}"/> </java> </target> </project>Our buildfile defines two properties. Regardless of where properties are defined, they are globally visible:
<property name="prop1" value="Property 1 from Buildfile"/> <property name="prop2" value="Property 2 from Buildfile"/>Properties are always name/value pairs, and can be overridden from the command line (shown shortly). They are referenced later in the buildfile using the ${prop1} and ${prop2} syntax. The run target echoes these property name/value pairs, and you can override them from the command-line:
<echo message="prop1 = ${prop1}"/> <echo message="prop2 = ${prop2}"/> <!-- The 'prop3' property must be defined on the command line or it shows up like '${prop3}' --> <echo message="prop3 = ${prop3}"/> <echo message="user.home = ${user.home}"/>As you can see, the buildfile tries to echo prop3 and user.home, even though they were not defined earlier. As the comment indicates, the value for prop3 must be specified on the command-line or it will be undefined. The user.home property is a standard Java system property, so it will have a default value.
Finally, the buildfile invokes a Java application, but passes only one of the properties:
<!-- pass one of the properties --> <sysproperty key="prop1" value="${prop1}"/>Now let's look at a little Java program that displays the same properties. Example 3-4 shows how you useSystem.getProperty( ) to retrieve system properties.
Example 3-4. Java application to print properties
package com.oreilly.javaxp; public class ShowProps { public static void main(String[] args) { System.out.println("Now in ShowProps class..."); System.out.println("prop1 = " + System.getProperty("prop1")); System.out.println("prop2 = " + System.getProperty("prop2")); System.out.println("prop3 = " + System.getProperty("prop3")); System.out.println("user.home = " + System.getProperty("user.home")); } }To tie this all together, let's look at some sample output. When the user types ant, they see the output shown next. This is the result of the default target, run, being executed.
[echo] Now in buildfile... [echo] prop1 = Property 1 from Buildfile [echo] prop2 = Property 2 from Buildfile [echo] prop3 = ${prop3} [echo] user.home = C:Documents and Settingsericb [java] Now in ShowProps class... [java] prop1 = Property 1 from Buildfile [java] prop2 = null [java] prop3 = null [java] user.home = C:Documents and SettingsericbAs you can see, prop3 is undefined in the buildfile because it was not specified on the command line. Theuser.home property is available because the Java runtime sets it for us. Once the demonstration enters theShowProps class, we see that properties are not automatically propagated from the Ant buildfile to Java applications. The value for prop1 is available to the ShowProps application because it was explicitly passed using <sysproperty>.
Here is the output when you type ant -Dprop1="First Prop" -Dprop3="Third Prop" on the command line:
[echo] Now in buildfile... [echo] prop1 = First Prop [echo] prop2 = Property 2 from Buildfile [echo] prop3 = Third Prop [echo] user.home = C:Documents and Settingsericb [java] Now in ShowProps class... [java] prop1 = First Prop [java] prop2 = null [java] prop3 = null [java] user.home = C:Documents and SettingsericbTo summarize, this shows how we can pass system properties from the command line to the Ant buildfile. Once inside the buildfile, we can use <sysproperty> to pass the properties to Java applications. This is a useful technique because we can use properties to avoid hardcoded values in buildfiles and Java programs.
3.6.4 See Also
See the JavaDoc for java.lang.System for a list of standard system properties. Use Ant's echoproperties task to list all properties in the current project.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
» |
Ant 로 Java Application 실행시 Target 에 파라미터를 입력하여 Arguments 로 전달하여 실행시키기
![]() | 황제낙엽 | 2012.06.05 | 2226 |
11 | ANT에서 Classpath 설정시 순서 주의 | 황제낙엽 | 2010.07.10 | 451 |
10 | Ant에서 "${java.class.path}"이 의미하는바 | 황제낙엽 | 2010.07.10 | 120 |
9 | ANT 를 이용한 RMI 컴파일 ( rmic 로 stub 생성하기 ) | 황제낙엽 | 2010.05.26 | 114 |
8 | Jakarta Ant II탄 상세편 | 황제낙엽 | 2007.01.24 | 113 |
7 | Jakarta Ant I탄 기본편 | 황제낙엽 | 2007.01.24 | 76 |
6 | 1900개가 넘는 java컴파일시 Ant의 설정 | 황제낙엽 | 2007.02.28 | 898 |
5 |
Ant 의 사용 용례와 사용법
![]() | 황제낙엽 | 2007.07.16 | 91 |
4 | Ant로 UTF-8 엔코딩하기 (프로젝트 변환) | 황제낙엽 | 2007.07.03 | 168 |
3 |
ANT 작성예제 (build.xml , build.properties)
![]() | 황제낙엽 | 2007.05.16 | 104 |
2 | Eclipse에서 Ant+xdoclet 를 이용한 web.xml, struts-config.xml 자동생성 | 황제낙엽 | 2007.03.03 | 219 |
1 | Eclipse 의 auto compile 과 ANT의 compile | 황제낙엽 | 2007.02.28 | 347 |