Gradle设置 Java项目classpath
来源:互联网 发布:淘宝二手单反镜头骗局 编辑:程序博客网 时间:2024/06/05 13:28
We’re building something great. Come help us. Join the Team!
Running java class from gradle build script
I need to run a main method on a java class from my build.gradle file. But how do avoid to specify the whole classpath manually like I am doing below:
task(runSimple, dependsOn: 'classes', type: JavaExec) { main = 'com.MyMain' classpath=sourceSets.main.runtimeClasspath classpath+=sourceSets.test.runtimeClasspath classpath+=sourceSets.main.resources classpath+=configurations.compile.dependencies // all dependencies for this project - is empty :-(}
The above does not work because the classpath does not contain the projects dependencies. Is there a way to specify that the classpath should be the one used when building this project which also contains all necessary dependencies?
If I add the following to my build.gradle file:
configurations.compile.dependencies.each { println it }
nothing is printed even though running this from commandline:
gradle dependencies
prints a dozen for the compile configuation.
created
Nov '12last reply
Jun '13- 4
replies
- 6.8k
views
- 4
users
- 5
links
Haven't tested this so a bit of freestyling here, but looking at the JavaExec API doc at:
206http://gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html#org.gradle.api.tasks.JavaExec:classpath206
we can see that the type for the 'classpath' property is FileCollection6. The DSL guide further tells us that theProject.configurations property is of type ConfigurationContainer3 and that expressions like:
configurations.compile
return values of type Configuration7. Reading the docs on this last link we can see that, quote:
Configuration is an instance of a FileCollection that contains all dependencies...
Ok, so what we need for the JavaExec.classpath is something of type FileCollection and that is exactly what the "configurations.compile" expression returns.
So to sum this up, I think the following should work for you:
task runSimple(type: JavaExec, dependsOn: 'classes') { main = 'com.MyMain' classpath = configurations.runtime}
replacing the 'runtime' part with 'compile' or whichever classpath you might need in your case,
@u1234: does Matias' response answer your question?
Not really. I can only make it work if I do it in the afterEvaluate scope and on the main sourceSet:
afterEvaluate{ main = 'com.MyMain' classpath=sourceSets.main.runtimeClasspath//classpath=configurations.runtime , does not work println "the classpath: " classpath.each { println it } }
With the above it finds the class but I get another error that a .properties file cannot be found which is located the test resources (I know that this should only be used when running tests):
test { java { srcDir 'test' } resources { srcDir 'testResources' } }
Therefore I have added the test resources like this:
afterEvaluate{ main = 'com.MyMain' classpath=sourceSets.main.runtimeClasspath classpath=+sourceSets.test.resources//classpath=configurations.runtime , does not work println "the classpath: " classpath.each { println it } }
but it gives an error:
groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.file.DefaultSourceDirectorySet.positive() is applicable for argument types: ()
Any suggestions on how to include resources from the test sourceSet when running the above class?
--create a new configuration "myMain"
-- to pickup the main classpath and the test classpath mymain.extendsFrom(test,main)
-- then classpath
classpath=configurations.myMain
Hey there! Looks like you're enjoying the discussion, but you're not signed up for an account.
When you create an account, we remember exactly what you've read, so you always come right back where you left off. You also get notifications, here and via email, whenever new posts are made. And you can like posts to share the love.
来自:https://discuss.gradle.org/t/running-java-class-from-gradle-build-script/6065
- Gradle设置 Java项目classpath
- Java classpath的设置
- Java如何设置ClassPath
- java中classpath设置
- JAVA初学者classpath设置
- Java中ClassPath设置
- java -jar classpath设置
- Java项目中的classpath
- 无需设置JAVA中的CLASSPATH
- 关于java的classpath设置
- 设置classpath运行java程序
- 关于java的classpath设置
- Java classpath 及 path 设置
- 命令行查看java classpath设置
- Java 环境变量设置 -- JAVA_HOME CLASSPATH
- 运行Java程序-设置classpath
- Java命令行设置classpath路径
- java的classpath的设置
- nssm在windows服务器上部署nodejs
- 新手引导感悟
- @RequestMapping测试各种访问方式
- Regular Expression Matching正则表达式匹配
- Java 负数转换成二进制
- Gradle设置 Java项目classpath
- SQLite3之补充
- 欢迎使用CSDN-markdown编辑器
- linux下安装redis3.2
- 输入正整数n,按从小到大的顺序输出所有形如abcde / fghij = n的表达式,其中a~j恰好为0~9的一个排列,2
- 其它常用类
- 判断一个点是否在指定区域内
- 【记录】eLua初体验
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法