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" run

Within 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 Settingsericb

As 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 Settingsericb

To 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.


번호 제목 글쓴이 날짜 조회 수
» Ant 로 Java Application 실행시 Target 에 파라미터를 입력하여 Arguments 로 전달하여 실행시키기 file 황제낙엽 2012.06.05 2278
47 Ant로 UTF-8 엔코딩하기 (프로젝트 변환) 황제낙엽 2007.07.03 1351
46 Jenkins Rest API 사용기 file 황제낙엽 2020.03.26 947
45 1900개가 넘는 java컴파일시 Ant의 설정 황제낙엽 2007.02.28 919
44 Eclipse 에서 생성한 Gradle Project 를 리프레쉬 했을때 주의할 점 황제낙엽 2023.11.30 916
43 [2014~2015] gradle 영문 튜토리얼 (Getting Started With Gradle) 황제낙엽 2023.07.13 749
42 ANT에서 Classpath 설정시 순서 주의 황제낙엽 2010.07.10 462
41 Eclipse 의 auto compile 과 ANT의 compile 황제낙엽 2007.02.28 348
40 [Maven] 로컬 레파지터리에 사용자 jar 추가하기 file 황제낙엽 2010.04.20 286
39 Eclipse 에서 Gradle Project 를 WAR 로 배포하기 황제낙엽 2023.12.01 236
38 Eclipse에서 Ant+xdoclet 를 이용한 web.xml, struts-config.xml 자동생성 황제낙엽 2007.03.03 222
37 Maven 툴을 이용한 효율적인 프로젝트 관리 방안 황제낙엽 2007.01.30 214
36 [Jenkins] Java Sample with Jenkins Restful API 황제낙엽 2020.08.12 205
35 Jenkins 백업 (Thinbackup) file 황제낙엽 2021.07.22 153
34 Jenkins Restful API로 Job 과 Build 정보 조회 황제낙엽 2020.09.02 145
33 Jenkins+TFS 연동 예제 황제낙엽 2020.09.01 144
32 jenkins에서 tfs에 접속하여 브랜치와 변경집합으로 소스를 다운로드하는 예제 황제낙엽 2020.08.11 141
31 Jenkins의 Restful API file 황제낙엽 2020.08.11 140
30 [ChatAI] Gradle Wrapper의 버전을 업그레이드하는 절차 황제낙엽 2024.11.12 138
29 Gretty, Jetty 에서 ssl 적용 황제낙엽 2024.11.12 132