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

huangapple go评论65阅读模式
英文:

Gradle Compile & Execute the Single Java Class with third-party jar dependencies using Gradle `buildscript`

问题

I followed this answer https://stackoverflow.com/a/19418847/1665592 but it doesn't help me to compile single java files using Gradle. It says Task :compileMessageKeys NO-SOURCE Why?

我按照这个答案https://stackoverflow.com/a/19418847/1665592,但它不能帮助我使用Gradle编译单个Java文件。它显示Task :compileMessageKeys NO-SOURCE,为什么?

I just have only one Java File CreateMessageKeysTask.java which is dependent on "apache-ant-1.7.0/ant.jar" jar

我只有一个Java文件CreateMessageKeysTask.java,它依赖于"apache-ant-1.7.0/ant.jar" jar

My ant script is as below

我的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>

Below are the class import statements used in my Java File.

以下是我的Java文件中使用的类import语句。

import org.apache.tools.ant.*

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

EDIT

I tried something below as well but it still not help me

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

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
}
英文:

I followed this answer https://stackoverflow.com/a/19418847/1665592 but it doesn't help me to compile single java files using Gradle. It says Task :compileMessageKeys NO-SOURCE Why?

I just have only one Java File CreateMessageKeysTask.java which is dependent on &quot;apache-ant-1.7.0/ant.jar&quot; jar

task compileMessageKeys (type: JavaCompile) {
    doFirst {
        println &quot;$projectDir/precompile&quot;
        new File(&quot;$projectDir/precompile&quot;).mkdirs()
    }
    source = sourceSets.main.java.srcDirs
    include &#39;mypackage.build.CreateMessageKeysTask.java&#39;
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}

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

My ant script is as below

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

Below are the class import statements used in my Java File.

import org.apache.tools.ant.*

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

EDIT

I tried something below as well but it still not help me

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

答案1

得分: 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目标时,它将只编译您在包含中添加的相应类。

这里是我的Gradle和目录结构的屏幕截图。
Gradle使用Gradle `buildscript`编译和执行带有第三方jar依赖的单个Java类。
在这里,您会看到我在src目录中有两个类,但编译后我只有一个类在输出中。

英文:

So I tried creating a separate sourceset, instead of configuring compile.. This is how I did.

sourceSets {
    single{
        java {
            srcDir &#39;src/main/java&#39;
            include &#39;**/Class1.java&#39;
        }
    }
}


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

Now, when I call compileSingle target, It will compile only respective classes you have added in includes.

Here are the screenshots from my gradle and directory structure.
Gradle使用Gradle `buildscript`编译和执行带有第三方jar依赖的单个Java类。
Here you will see that I have two classes in my src directory, but post compile I have only one class in the output.

答案2

得分: 1

以下是翻译好的部分:

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

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

It is pretty simple in to compile and execute java code in Gradle with buildscript

buildscript {
    dependencies {
        classpath files(&#39;lib/apache-ant-1.7.0/ant.jar&#39;)
    }
}


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

huangapple
  • 本文由 发表于 2020年8月7日 19:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定