Gradle设置 Java项目classpath
2016-07-01 18:12
1846 查看
We’re building something great. Come help us.
Join the Team!
The Gradle team is pleased to announce Gradle 2.14.1-rc-1
The Gradle team has an important update to 2.14. This release fixes acritical defect to incremental builds that may prevent Gradle from executing tasks when inputs or outputs are out of date. This affects the correctness of builds using Gradle
2.14.
This release also contains two minor fixes to the Ear and CodeNarc plugins.
It does not include any new features. We strongly recommend anyone upgrading from an older version to upgrade to Gradle 2.14.1 instead of Gradle 2.14.
Enjoy the new version and let us know what you think!
Check the
2.14.1 release notes for more information.
For further information about the last minor release, check the
2.14 release notes for more information.
Upgrade Instructions
Switch your build to use Gradle 2.14.1 quickly by updating your wrapper properties:./gradlew wrapper --gradle-version=2.14.1-rc-1
Standalone downloads are available at https://gradle.org/gradle-download.
Reporting Problems
If you find a problem with Gradle 2.14.1, please post a reply to this topic or create a new topic inBugs. Be sure to include which operating system you are using, the version of Gradle you upgraded from and any steps you have found that reproduces your problem.
Old
Forum
u1234tulsoba
Nov '12
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 '12
last reply
Jun '13
4
replies
6.8kviews
4users
5links
mbjarland1Matias
Bjarland
Nov '12
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 the
Project.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,
luke_daley
Nov '12
@u1234: does Matias' response answer your question?
u1234tulsoba
Nov '12
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?
6 months later
Gail_Stewartgstewart
Jun '13
--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
相关文章推荐
- Java 负数转换成二进制
- Dubbo包冲突:java.lang.reflect.MalformedParameterizedTypeException
- java 基础
- 2016(3-6)知识总结
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之八:一个简单例子演示Spring-MVC中多种参数传递方式
- java设计模式之---动态代理
- Eclipse里面能执行,但命令行里不能执行class文件
- Java批量转换文件夹中文件的编码(从gbk到utf8)
- java常用容器总结
- springMVC+spring+Mybatis集成
- Spring MVC配置介绍
- eclipse中常用的快捷键
- java获取系统时间日期
- Maven3.0+Spring MVC4+Spring 4+Mybatis3+junit4
- spring+SpringMVC+Mybatis框架整合
- java之断点续传简单实现
- Java WebService 简单实例
- Spring MVC File Download Example
- Java基础理论--异常处理
- spring security3 实现踢出在线用户