문제

내 SRC/ Tree (그리고 아마도 내 테스트/ 트리에) 소스가 있다고 가정 해 봅시다. 컴파일하고 싶다고 말합니다 부분 그 나무의. 내가하고 싶은 이유는 다양합니다. 예를 들어, 특정 클래스를 포함하지 않고 가장 작은 항아리를 만들고 싶을 수도 있고, 컴파일하고있는 가장 빠른 컴파일 시간을 원할 수도 있습니다. 그래도 모든 종속성을 컴파일하고 싶습니다!

이것은 명령 줄에서 쉽게 달성 할 수 있습니다.

javac -d build/ -cp whatever -sourcepath src src/path/to/MyClass.java

이제 Ant와 어떻게 할 수 있습니까? Javac 개미 작업은 모든 것을 컴파일합니다:

Java 소스 파일을 컴파일 할 수 있도록 소스 및 대상 디렉토리는 재귀 적으로 스캔됩니다.

하나는 사용할 수 있습니다 excludes 그리고 includes 매개 변수이지만이 목적에 문제가 있습니다. 사실, 모든 것을 명시 적으로 설정 해야하는 것 같습니다. includes (자동 의존성 조회가 아님) 및 심지어 최악 저것 제외는 포함에 우선 순위가 있습니다:

포함과 제외가 모두 사용될 때, 포함 패턴 중 하나 이상 일치하고 일치하지 않는 파일/디렉토리 만 어느 제외 패턴의 사용이 사용됩니다.

따라서 사용할 수 없습니다

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
            excludes="**/*.java" includes="src/path/to/MyClass.java" />  

아무것도 컴파일하지 않기 때문에 :-(

간단한 명령 줄을 달성하는 방법이 있습니까? javac 개미와 함께?


편집 : 답변 해주셔서 감사합니다, 새디, 나는 그것을 받아들이고 있습니다.이 질문에서 궁금해하는 방식으로 작동하기 때문입니다. 그러나 나는 몇 가지 의견이 있습니다 (당신의 답변의 의견 필드에 너무 오래) :

1) 문서를 읽었지만 (위의 링크 참조) includes 당신은 실제로 다른 모든 것을 제외하고 있습니다

2) 당신이 그냥 includes Ant Logs와 비슷합니다

[javac] Compiling 1 source file to /my/path/to/build

종속성이 하나의 소스 파일 이상을 컴파일하는 경우에도 마찬가지입니다.

도움이 되었습니까?

해결책

Why are you excluding as well as including? If you have at least one include, then files are only compiled if they're explicitly included. So this should work:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
        includes="src/path/to/MyClass.java" />

Or more flexibly:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
    <include name="src/path/to/MyClass.java"/>
    <include name="src/path/to/AnotherClass.java"/>
</javac>

To include only certain packages or classes in a jar, use a fileset attribute

<jar jarfile="${outlib}/something.jar">
    <fileset dir="${build.dir}">
        <include name='src/path/to/classes' />
    </fileset>
</jar>

Again, you can use multiple includes to combine separate packages. Experiment with includes and read the documentation and you're sure to find the answer you need.

다른 팁

Old question, but I was struggling with the same problem and found a a more elegant solution. So here it is, for future reference:

According to the ant docs the <javac> element is an implicit <fileset> and as such can take Selectors like <filename name="**/MyClass.java"/>, so this would only compile MyClass.java:

<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath">
  <filename name="**/path/to/MyClass.java"/>
</javac>

Actually, ant only checks everything, if you run a compile twice in a row you will notice the second is much quicker. Actually, it can be easily persuaded to miss things.

If you don't even want it to consider everything, you're going to have to break it down into smaller modules/projects/source trees so that you're explicitly telling ant what to compile.

Just give only the comma seperated list of files that you want to build. Lets say example

<property name="includeFileList" value="<name of java class>.java"/>

<javac srcdir="${src.dir}" destdir="${build.dir}"
       target="1.6" debug="true" includes="${includeFileList}"/>

It will work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top