如何用gradle编译单个java文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19391872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:45:31  来源:igfitidea点击:

How to compile a single java file with gradle?

javagradle

提问by I Stevenson

(UPDATED: Thanks to comments from Peter N below.)

更新:感谢下面 Peter N 的评论。)

I have a java package with several classes and therefore files within it. However I only want to compile one of those files initially so that I can then use it to update the other classes in the package. It's a factory for the other classes and as a result is dependent on them - ie. it has references to them.

我有一个包含多个类的 java 包,因此其中包含文件。但是,我最初只想编译这些文件之一,以便我可以使用它来更新包中的其他类。它是其他类的工厂,因此依赖于它们 - 即。它有对它们的引用。

I've been attempting to use a JavaCompile gradle taskto do this. As a result I've read the documentation for JavaCompile and attempted to search for examples but seems there is very little out there. I've even posted on the gradle forums, but slow going so far.

我一直在尝试使用JavaCompile gradle 任务来执行此操作。因此,我阅读了 JavaCompile 的文档并尝试搜索示例,但似乎很少有。我什至在gradle 论坛发帖,但到目前为止进展缓慢。

I'm able to do what is required readily with an ant script using the ant javac task. But I'd like to do it in gradle (and without using the ant.javac method - if I have to do that I'll just use ant).

我可以使用 ant javac 任务使用 ant 脚本轻松完成所需的操作。但我想在 gradle 中完成(并且不使用 ant.javac 方法 - 如果我必须这样做,我将只使用 ant)。

I've created an example case with just two source files for two classes in the one package.

我创建了一个示例案例,其中一个包中的两个类只有两个源文件。

Both files are in a package called some.pkg and we therefore have the following directory structure:

这两个文件都在一个名为 some.pkg 的包中,因此我们具有以下目录结构:

  • .\build.gradle
  • .\src\main\java\some\pkg\ClassOne.java
  • .\src\main\java\some\pkg\ClassTwo.java
  • .\build.gradle
  • .\src\main\java\some\pkg\ClassOne.java
  • .\src\main\java\some\pkg\ClassTwo.java

ClassOne:

第一类:

package some.pkg;

import some.pkg.*;

public class ClassOne {
    public void doStuff() {
    }
}

ClassTwo:

类二:

package some.pkg;

import some.pkg.*;

public class ClassTwo {
    public void ClassTwo() {
        ClassOne cone = new ClassOne();
        cone.doStuff();
    }
}

As you can see, ClassTwo (our class we want to compile standalone) depends on ClassOne.

如您所见,ClassTwo(我们要独立编译的类)依赖于 ClassOne。

The ant script is a simple case of:

ant 脚本是一个简单的例子:

<project>
    <property name="build.dir"     value="build"/>
    <property name="lib.dir"     value="lib"/>
    <property name="src.dir"     value="src/main/java"/>
    <target name="generate" >
        <mkdir dir="${build.dir}"/>
        <javac source="1.6" target="1.6" srcdir="${src.dir}" destdir="${build.dir}" 
               debug="true" includeantruntime="false"
               includes="some/pkg/ClassTwo.java">
        </javac>
    </target>
</project>

But in gradle, I keep having an issue where javac (or JavaCompile task) can not find ClassOne. It's as though the sourcedir is not pointing where it should - and indeed as though I was just running javac on the command line. I was thinking the gradle 'JavaCompile' task's 'source' property was working like the ant 'srcdir' property, but that appears to not be the case. So here is the gradle script I'm currently trying:

但是在 gradle 中,我一直遇到 javac(或 JavaCompile 任务)找不到 ClassOne 的问题。就好像 sourcedir 没有指向它应该指向的位置 - 实际上就像我只是在命令行上运行 javac 一样。我在想 gradle 'JavaCompile' 任务的 'source' 属性就像 ant 'srcdir' 属性一样工作,但事实并非如此。所以这是我目前正在尝试的gradle脚本:

apply plugin: 'java'

task compileOne (type: JavaCompile) {
    source = sourceSets.main.java.srcDirs
    include 'some/pkg/ClassTwo.java'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}

But it results in:

但结果是:

C:\test\>gradle generate
:generate
C:\test\src\main\java\some\pkg\ClassTwo.java:7: cannot find symbol
symbol  : class ClassOne
location: class some.pkg.ClassTwo
                ClassOne cone = new ClassOne();
                ^
C:\test\src\main\java\some\pkg\ClassTwo.java:7: cannot find symbol
symbol  : class ClassOne
location: class some.pkg.ClassTwo
                ClassOne cone = new ClassOne();
                                    ^
2 errors
:generate FAILED

So how do I achieve an ant javac equivalent in gradle?

那么如何在 gradle 中实现 ant javac 等价物呢?

antjavac seems to have the smarts to go and compile all the other related classes, but I guess for gradle JavaCompile I will need to set up a sourceSet to do that.. not sure..

antjavac 似乎有智慧去编译所有其他相关的类,但我想对于 gradle JavaCompile 我需要设置一个 sourceSet 来做到这一点..不确定..

Thanks!

谢谢!

采纳答案by I Stevenson

Thanks to the discussion with @PeterNiederwieser on the original post in the comments, I'll provide the answer here for completeness.

感谢与@PeterNiederwieser 就评论中的原始帖子进行的讨论,为了完整起见,我将在此处提供答案。

To have gradle JavaCompile function in a manner very similar to the ant javac, you need to provide the sourcepathcompiler option via the options.compilerArgsproperty. Therefore, the gradle script that now works is as follows:

要以与 ant javac 非常相似的方式使用 gradle JavaCompile 函数,您需要sourcepath通过options.compilerArgs属性提供编译器选项。因此,现在可以运行的gradle脚本如下:

apply plugin: 'java'

task compileOne (type: JavaCompile) {
    source = sourceSets.main.java.srcDirs
    include 'some/pkg/ClassTwo.java'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}

compileOne.options.compilerArgs = ["-sourcepath", "$projectDir/src/main/java"]

Note specifically the last line (the only difference) which allows all to work. The result of which is that it will actually compile both ClassOne and ClassTwo at build time - rather than only attempting the single explicit file you specified. Any other classes (that are not required) remain uncompiled - as confirmed by looking in the build directory.

特别注意最后一行(唯一的区别),它允许所有人工作。其结果是它实际上会在构建时同时编译 ClassOne 和 ClassTwo - 而不是仅尝试您指定的单个显式文件。任何其他类(不是必需的)保持未编译状态 - 通过查看构建目录确认。

Thanks Peter!

谢谢彼得!