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.


번호 제목 글쓴이 날짜 조회 수
49 GitHub, SourceTree, Gradle, Eclipse 환경의 java project 생성하기 절차 황제낙엽 2025.08.26 252
48 eclipse 용 gradle web application project (no spring) 생성하기 황제낙엽 2025.05.04 317
47 라이브러리 개발 프로젝트에서 jar 로 build 후 파일을 특정 로컬 경로로 copy 하는 예제 황제낙엽 2025.02.07 294
46 [Gemini] gradle init 으로 프로젝트 생성하는 절차 내용 황제낙엽 2025.01.24 344
45 [ChatGPT] eclipse 의 web project 에서 gradle refresh 때마다 context root 가 초기화 황제낙엽 2024.11.14 375
44 Gretty, Jetty 에서 ssl 적용 황제낙엽 2024.11.12 414
43 [ChatAI] Gradle Wrapper의 버전을 업그레이드하는 절차 황제낙엽 2024.11.12 458
42 eclipse 용 gradle web application sample project (no spring) file 황제낙엽 2024.11.12 374
41 [phind] Gradle 프로젝트를 생성할 때 nested sub project 가 함께 생성되는 이유 황제낙엽 2024.11.12 362
40 IDE(IntelliJ, Android Studio, Eclipse) 에서 build.gradle 에 dependencies 추가후 의존 라이브러리 갱신하기 황제낙엽 2024.10.30 379
39 [POST, 2021.06.28] Gradle을 이용해 Web 어플리케이션을 만들고 빌드하고 실행하기 file 황제낙엽 2023.12.30 330
38 build.gradle 의 dependencies 황제낙엽 2023.12.29 327
37 error: unmappable character 황제낙엽 2023.12.29 326
36 Task :compileJava FAILED 황제낙엽 2023.12.29 349
35 사용자 라이브러리 파일(*.jar) 를 의존성에 추가하는 방법 황제낙엽 2023.12.28 361
34 Windows 에 Gradle 설치 황제낙엽 2023.12.23 347
33 Eclipse, Gradle 프로젝트 생성, Dynamic Web Project 로 변경 황제낙엽 2023.12.23 362
32 Eclipse 에서 Gradle Project 를 WAR 로 배포하기 황제낙엽 2023.12.01 538
31 Eclipse 에서 생성한 Gradle Project 를 리프레쉬 했을때 주의할 점 황제낙엽 2023.11.30 1532
30 Gradle 기본 다지기 1편 file 황제낙엽 2023.11.16 244