I'm developing a project with gradle. My build file is almost empty so far:
apply plugin: 'java'
apply plugin: 'eclipse'
version = '0.1'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
}
My project depends on a Maven project. Precisely this project: http://git.eclipse.org/c/bpmn2/tree/org.eclipse.bpmn2
I've cloned this project into my workspace, but I don't know the best way to declare the dependency in my build.gradle file. This is what I've done so far:
dependencies {
compile files ("C:/path/to/org.eclipse.bpmn2-0.7.0-SNAPSHOT.jar")
}
But this way I have to manually build the maven project. Does somebody know a better way of doing this dependency management?
I'm using Eclipse Gradle Integration and I've noticed an interesting eclipse project property:
Gradle - Dependency Management
[x] Remap Jars to maven projects (requires Gradle 1.1 and m2e)
This seems to do what I need. But I don't know how to use this feature...
Thanks in advance.
If the Maven project is not available in any Maven repo, Gradle can't find it anywhere, so you'll have to build it. I would at least mvn install it, and tell Gradle to look for artifacts in your local Maven repo rather than in a specific directory, using
repositories {
mavenLocal()
}
The eclipse-integration-gradle plugin replaces the mavenLocal() jar dependency with a Eclipse project dependency. This is the easiest way I've found so far. See: http://forum.springsource.org/showthread.php?139634-How-to-use-quot-remap-Jars-to-maven-projects-quot-feature
Related
this is my build.gradle
repositories {
mavenCentral()
maven{url 'http://example.com/repository/maven-public/'}
}
dependencies {
compile group: 'com.example', name: 'example-commlib', version: '1.0'
}
and the link http://example.com/repository/maven-public/ is build from Nexus Repository Manager which can accessable.
and when I click Refresh Gradle Project in eclipse.
I can see the link is point to
https://repo.maven.apache.org/..../example-commlib-1.0.pom
I think the right url is
http://example.com/repository/maven-public/.../example-commlib-1.0.pom
I've check the gradle setting and maven setting in eclipse. It seems fine.
So what's the problem?
Do you have other dependencies on this project? It is possible that the "pom.xml" file of one of the packages you are pulling references example-commlib as a dependency and has the Apache Maven URL hardcoded, and resolves the URL beforehand. You can use gradle dependencies to show the dependency tree and find which version is affected. The answers in this post have suggestions on how to force a specific dependency as well: How can I force Gradle to set the same version for two dependencies?
I need some retrofit2 dependencies for my java project.
In build.gradle additional repositories are declared:
repositories
{
jcenter()
mavenCentral()
mavenLocal()
}
In dependencies section I declared:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-jackson:2.1.0'
So Gradle could not find them.
I used project-report plugin to analyze dependency tree. I got next report:
Interesting thing that before I used 2.0.2 versions for retrofit libraries and Gradle found them.
Any help will be appreciated.
Gradle version 2.12
If using IntelliJ you can include plugin idea or include plugin eclipse, which gives you an option to run gradle idea or gradle eclipseClasspath tasks and refresh the project with updated/freshly added dependencies.
IDE's are not yet ideal supporting gradle.
I am writing a Java library and I would like to build the library with Gradle and then test it from a local test project.
I would prefer using Gradle 3.3 for my objective.
The library should be built for Java5 and higher.
So far my build.gradle looks like this:
plugins {
id 'jvm-component'
id 'java-lang'
}
repositories {
mavenCentral()
}
model {
components {
main(JvmLibrarySpec) {
sources {
java {
dependencies {
module 'commons-codec:commons-codec:1.10'
module 'org.apache.httpcomponents:httpcore:4.4.6'
module 'org.apache.httpcomponents:httpclient:4.5.3'
}
}
}
api {
exports 'io.simplepush'
}
targetPlatform 'java5'
}
}
}
The source code of the library is located in src/main/java/io/simplepush/Notification.java and depends on the dependencies stated in the build.gradle file.
Building the library with ./gradlew build works fine and generates build/jars/main/jar/main.jar.
However when I run a test project from IntelliJ (after including main.jar into the test project), I get the following runtime error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity.
It seems like the test project does not know about the runtime dependencies needed by my library.
I am not sure on what is the correct way to tell the test project about the dependencies of my library.
I do not want a fat jar which includes all dependencies.
Listing all dependencies in the test project itself is also not an option.
Preferably I want the library itself to tell the test project about which dependencies it needs.
The library jar which you have created does not contain any dependency information which the IDE/Gradle can then resolve to be able to compile/run the test project. I see that you are using the maven central repository so what you need to do is to publish your library to your local maven repository and in the test project just add a dependency information (no just plain jar file).
So in both library and test project build.gradle add a maven local repository config.
repositories {
mavenLocal()
mavenCentral()
}
And now you need to publish the library to local repository. As you are using the gradle 3.3 you can use the Maven Publishing.
So in the library build.gradle add a maven publishing information.
publishing {
publications {
maven(MavenPublication) {
groupId 'io.simplepush'
artifactId 'project1-sample'
version '1.1'
from components.java
}
}
}
Gradle “maven-publish” plugin makes this easy to publish to local repository automatically creating a PublishToMavenLocal task.
So you can just run
gradle publishToMavenLocal
Which will publish your library with all the dependency information into local maven repository.
And then you just need to add a library information to you test projects build.gradle
dependencies {
// other dependencies .....
module 'io.simplepush:project1-sample:1.1'
}
I solved it by changing several things.
Thanks to #Babl for pointing me in the right direction.
My new library build.gradle looks like this:
plugins {
id 'java'
id 'maven-publish'
}
sourceCompatibility = 1.5
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'commons-codec:commons-codec:1.10'
compile 'org.apache.httpcomponents:httpcore:4.4.6'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
}
publishing {
publications {
maven(MavenPublication) {
groupId 'io.simplepush'
artifactId 'project1-sample'
version '1.1'
from components.java
}
}
}
Now I can push the library to the local maven repository with ./gradlew publishToMavenLocal.
The build.gradle of the test project uses the application plugin and defines a main class (which is Hello in my case). Then I can run ./gradlew installDist to generate an executable file (see Application plugin docs) which puts all dependencies in the classpath and runs just fine.
group 'com.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'io.simplepush:project1-sample:1.1'
}
mainClassName = "Hello"
This specify what repositories to check to fetch the dependencies from
repositories {
mavenCentral()
}
Therefore, anything that is in the dependecies{} will be fetched from those above.
If the test project is not coupled with the library project, (#RaGe example) new test project needs to know where to take the dependency from - you need to publish it, using preferred method.
After that, your new test project needs to specify the library with the preferred configuration (compile...runtime etc) in the build.gradle dependencies{}
After that depending on IDE you need to refresh the classpath and download the dependency from the specified before repository, the transitive dependencies specified in the library dependency (in this case) will get fetched from test projects repositories{}
Library build.gradle
repositories {
mavenCentral()
}
dependencies {
module 'commons-codec:commons-codec:1.10'
module 'org.apache.httpcomponents:httpcore:4.4.6'
module 'org.apache.httpcomponents:httpclient:4.5.3'
}
test project build.gradle
repositories {
mavenCentral() repository to fetch transitives
mavenLocal() or any other repo that you published the library to
}
dependencies {
pref-conf librarygroup:name:version
}
You can use idea or eclipse plugin in gradle for gradle idea or gradle eclipseClasspath tasks to refresh it with your freshly added dependencies.
With this solution, you should not need to pack the transitive dependencies within the library,
PS. I am just confused after you said you want executable jar.
I am using Gradle in a new IntelliJ project. We have an internal Sonatype Nexus repository and I have declared that in build.gradle. I also added the dependency to the build script.
group 'com.companyName'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven{
url 'http://host:port/nexus/content/repositories/releases'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'com.companyName', name:'abc', version: '2.2.7'
}
As you can tell this is a brand new project. IntelliJ will build the project but wont resolve the external dependency. When I look in the Gradle projects view in IntelliJ there is a red squiggly line under the dependency and it says Unable to resolve dependency. I know the dependency exists and I can use it in Maven projects.
I've search around without any solution, tried all different settings in the intelliJ project also. Any ideas?
From discussion comments on the original post:
There seem some caches be corrupt.
Delete the .gradle folder in your project and ~/.gradle/caches, then try to resolve the dependencies again.
I had a similar problem with IntelliJ 2017.1 connecting to Artifactory. I opened a Terminal, ran "./gradlew tasks" and, as a side-effect, the dependency was downloaded.
Issues like unable to resolve dependencies(even after checking that there are no typos) could be because of firewall settings.
Check with your security department on how to fix it.
In some organizations, they've local mirror. In gradle.properties you'll have to give that path in distributionUrl
Also in build.gradle use corresponding path in repositories.
When trying to run the idea plugin for my gradle project, a number of my intellij libraries are in error with Library: 'gradle: unresolved_dependency_blah_blah' has broken classes path. the library itself is marked as "unresolved dependencies"
My gradle project is a multi module, I have applied the idea plugin to allprojects.
Intellij version is #IU-134.1007
gradle version is 1.11
Any help would be appreciated.
UPDATE: these are an example of some of the dependencies i have:
apply plugin: 'groovy'
dependencies {
compile 'org.eclipse.jgit:org.eclipse.jgit:3.2.0.201312181205-r'
compile 'org.codehaus.groovy:groovy:2.2.1'
testCompile 'org.junit:junit:4.11'
}
all three have the errors
Solved...
The issue was that i had not declared any repositories. (thank you peter)
adding the following to my root project resolved the dependencies correctly:
allprojects {
repositories { mavenCentral() }
}