英文:
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 "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"]
My ant
script is as below
<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.
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
}
答案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和目录结构的屏幕截图。
在这里,您会看到我在src目录中有两个类,但编译后我只有一个类在输出中。
英文:
So I tried creating a separate sourceset, instead of configuring compile.. This is how I did.
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]
}
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.
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('lib/apache-ant-1.7.0/ant.jar')
}
}
task CreateMessageKeys {
def obj = new mypackage.build.CreateMessageKeysTask();
obj.execute();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论