使用Gradle `buildscript`编译和执行带有第三方jar依赖项的单个Java类

5
我按照这个答案 https://dev59.com/4GIk5IYBdhLWcg3wUsnv#19418847 操作,但是它并不能帮助我使用Gradle编译单个Java文件。它显示 Task :compileMessageKeys NO-SOURCE 为什么?
我只有一个Java文件CreateMessageKeysTask.java,它依赖于"apache-ant-1.7.0/ant.jar" jar包。
task compileMessageKeys (type: JavaCompile) {
    doFirst {
        println "$projectDir/precompile"
        new File("$projectDir/precompile").mkdirs()
    }
    source = sourceSets.main.java.srcDirs
    include 'mypackage.build.CreateMessageKeysTask.java'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}

compileMessageKeys.options.compilerArgs = ["-sourcepath", "builder/precompile-task/src/mypackage/build"]

我的ant脚本如下所示

  <target name="precompile"
          description="builds the CreateMessageKeysTask to generates the MessageKeys interface">
    <mkdir dir="${precompile}" />
    <echo message=" precompile.src: ${precompile.src}" />
    <javac srcdir="${precompile.src}" destdir="${precompile}"
           includes="mypackage/build/**"
           debug="true" fork="true" memoryMaximumSize="${javacCoreMaxMem}"
           includeantruntime="false">
      <classpath>
        <fileset dir="${my.proj.base}/lib/">
          <include name="apache-ant-1.7.0/ant.jar" />
        </fileset>
      </classpath>
    </javac>
  </target>

以下是我Java文件中使用的类import语句。
import org.apache.tools.ant.*

import java.io.*;
import java.util.*;

编辑

我尝试了下面的方法,但仍然没有帮助我。

task compileMessageKeys (type: JavaCompile) {
    doFirst {
        println "$projectDir/precompile"
        new File("$projectDir/precompile").mkdirs()
    }
    dependencies{
        compile fileTree(dir: 'lib/', include: ['apache-ant-1.7.0/ant.jar'])
    }
    source = sourceSets.main.java.srcDirs
    include 'mypackage.build.CreateMessageKeysTask.java'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}

首先,include 接受一个 Ant 风格的文件模式,但你使用的是类似于 Java 包引用的东西。然而,总体来说,我建议你创建一个 Gradle 任务,可能在 buildSrc 中,来管理这个问题。 - Bjørn Vester
同意...但我的 ant task 依赖于一些 ant implementation https://stackoverflow.com/q/63339476/1665592.. 我正在寻找它 - Swapnil Kotwal
我可以使用 task runWithJavaExec(type: JavaExec) { } 来编译 Java 文件并执行它吗? - Swapnil Kotwal
2个回答

3

所以我尝试创建一个独立的源集,而不是配置编译...这就是我的做法。

sourceSets {
    single{
        java {
            srcDir 'src/main/java'
            include '**/Class1.java'
        }
    }
}


task compileSingle(type: JavaCompile) {
    source = sourceSets.single.java
    classpath = sourceSets.main.compileClasspath
    destinationDirectory = sourceSets.main.output.classesDirs[0]
}

现在,当我调用compileSingle目标时,它只会编译你在 includes 中添加的相应类。
这是我的 Gradle 和目录结构截图。 enter image description here 在这里,你可以看到我的 src 目录中有两个类,但是编译后,输出中只有一个类。

我添加了以下代码:task runScript(dependsOn: 'compileSingle', type: JavaExec) { main = 'CreateMessageKeysTask' classpath = sourceSets.main.compileClasspath }但是出现了错误:Could not find or load main class CreateMessageKeysTask - Swapnil Kotwal
我认为在sourceset上,你将不得不始终使用单个源。 - Anand Vaidya
我执行了一个Java文件,它为我生成了另一个“message.propertis”文件... 编译后,我需要执行它,以便可以生成“message.propertis”文件。 - Swapnil Kotwal
你的类路径应该是 sourceSets.single.compileClasspath。 - Anand Vaidya

1

使用buildscriptGradle中编译和执行Java代码非常简单。

buildscript {
    dependencies {
        classpath files('lib/apache-ant-1.7.0/ant.jar')
    }
}


task CreateMessageKeys {
    def obj = new mypackage.build.CreateMessageKeysTask();
    obj.execute();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接