I'm not used to post questions so please excuse my mistakes.
I'm looking into moving to Gradle+Nexus for my company and I've hit a wall.
I will try to describe my problem as clearly as possible.
I wrote 2 simple java programms, one depends on the other, both depends on junit to run theirs tests.
I could compile, test and run them while I was using Maven repository but one of the goals of using Nexus is to have 3rd party libraries on it as well as ours. While using Maven I could upload my builds to Nexus and also use them to compile with (through the dependencies) so no problem there.
But when I downloaded junit and uploaded it to Nexus I could not compile anymore.
Here is the error message I got:
Could not resolve all dependencies for configuration ':Project2:testCompile'.
>Could not find junit.jar (junit:junit:4.12).
Searched in the following locations:
http://mycompany/nexus/content/repositories/TRUNK_3rdParty/junit/junit/4.12/junit-4.12.jar
So gradle looks at the right place but cannot find junit.
I should mention that I uploaded junit using the POM file provided by Maven so everything should work.
As it might be of help, here are some code fragments from my build.gradle file:
repositories {
maven {
url "http://mycompany/nexus/content/repositories/TRUNK/"
}
}
dependencies {
compile group: 'mycompany', name: 'project1Nexus', version: '1.0-TEST'
}
that tells project2 where to find project1 on Nexus in order to compile (it works).
repositories {
maven {
url "http://mycompany/nexus/content/repositories/TRUNK_3rdParty/"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
and that should tell where to find junit but I get this error message.
Also the first repo/depend block is from the build.gradle file of project2 and the second one is from the one of the root folder. I doubt this could be an issue though as it worked fine when I used centralMaven.
If anyone had some insights to share, it would be amazing!
Cheers
Related
I know this has been asked multiple times but the questions have multiple answers.
I'm trying to use a Java package that's a dependency of my dependency. Let's say I've built this gradle project called "ee_tools". And that has a dependency called "my_models", which has a package called "com.mycompany.my_models.db". So in the ee_tools project build.gradle, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "my_models", version: "1.0.0"
}
Then in my current project, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "ee_tools", version: "1.0.0"
Shouldn't this mean that the public classes in my_models are accessible through ee_tools to my current project? Gradle was able to find both in my artifactory instance. And the gradle dependencies command shows ee_tools under the compileClasspath, implementation, and testCompileClasspath trees, but not with its children dependencies. It also shows up in the runtimeClasspath and testRuntimeClasspath trees with its children dependencies, including my_models.
I am also able to see that package inside the jar on the left side of IntelliJ, under the "External Libraries" tree, along with some classes.
But when I try to use the my_models package in my current project, IntelliJ can't find it and it fails a gradle build with the error
error: package com.company.my_models.db does not exist
It can't find any package in that dependency. What gives? Am I declaring the dependencies incorrectly? Or is this a gap between the gradle binary in my command line vs IntellJ and gradlew?
If ee_tools depends on my_models, then your gradle file in ee_tools should be like
implementation project(path: ":path:to:my_models", configuration: 'default')
:path:to:my_models is defined in settings.gradle in project root path like this:
include ':path:to:my_models'
I have a problem where I’m trying to include org.json as a dependency in my gradle/spring boot project. In my build.gradle I have
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.json:json:20141113")
}
But in the editor window it looks like IntelliJ is not able to import org.json - it’s unable to find the package
What’s weird is that I can run the gradle ‘build’ task without errors in IntelliJ, and I can run
./gradlew clean build && java -jar build/libs/MyApp-0.1.0.jar
and it works fine.
Is this a ‘fat jar’ problem? I’m not sure it is because org.json is definitely not in my class path on the command line. Any advice appreciated.
I was able to successfully download json in my gradle dependencies:
implementation 'org.json:json:20171018'
As some mentioned, mvnrepository provides snippet for different Build systems.
After you choose your desired version, in the middle of the page, select Gradle as build system, copy and paste the snippet in your build.gradle file, section dependencies in your java project.
// https://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '20180813'
I am using Gradle for a project. My build.gradle has got JUnit as a dependency, but every time I import the project again it does not recognize the JUnit library and asks me to add it to classpath. This problem does not only apply to JUnit, but also to other dependencies. Did I add JUnit to the wrong place?
PS: The project builds just fine, it's just the IDE (IntelliJ) that's marking everything red.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The dependency declaration is correct. Probably you're opening the project in IntelliJ IDEA the wrong way.
I suggest you to open a Gradle project this way:
Click the File | Open menu
Select the build.gradle file
Here is further information about importing Gradle projects (see also the side note in that page).
download the external jar file from mvn repository and add it to libs folder.
Gradle-project(your project)->libs(inside libs folder paste jar file). if you don't have libs folder just create it.
go to build.gradle
dependencies { compile files('libs/your_jar.jar') }
reload the gradle project.
it's worked for me. i hope it's work for you guys also...
I maintain at least 2 Java Gradle projects. Let's say that I have a common project named A and another project B that depends on A. A is technically common to many other projects, but that isn't a minor detail to my problem. Anyways, the current situation is that B declares a dependency on A as a plain-old external dependency in its build.gradle file, like so:
compile group:'com.example', name: 'A', version: '0.1'
We have code specific to tests in A that we share with B by placing it in src/main/java. This code has to stay in A instead of B because there are other projects depending on A that use this test code. I'd like to avoid putting the code in this directory because it has no purpose being deployed to production, due to its test-only nature. Also, simply moving the code from A to B wouldn't be possible because other projects in my organization depend on the same code and I want to avoid code duplication. I would rather move this test code in A to src/test/java, but then it won't be published to A's JAR file.
Thus, I am trying to pursue a solution where this code lives in src/test/java in A and is deployed in a test-only JAR file. There are posts that discuss possible solutions, such as this blog post or SOF posts such as Multi-project test dependencies with gradle and Multi-project test dependencies with gradle. HOWEVER, I continuous run into a problem caused by the fact that I am not maintaining a multi-project Gradle build, but rather two completely separate single-project Gradle builds, and this cannot change.
Where I am at right now is that I am configuring build.gradle for A in the exact way suggested in the blog post, like so:
configurations {
testOutput.extendsFrom testCompile
}
task jarTest (type: Jar, dependsOn: testClasses) {
from sourceSets.test.output
classifier = 'test'
}
artifacts {
testOutput jarTest
}
And then I have A declared as both compile and testCompile dependencies in build.gradle of B:
compile(group: 'com.example', name: 'A', version: '0.1')
testCompile(group: 'com.example', name: 'A', version: '0.1', configuration: 'testOutput')
This doesn't work. I am not getting any of the testCompile dependencies visible in the test classpath for B; these testCompile dependencies are not being reported by gradle dependencies nor are recognized by my IDE's code search (IntelliJ). I threw up a Hail Mary and even tried to replace configuration: 'testOutput' with classifier: 'test', but to no avail. Gradle documentation doesn't quite seem to help either, as it seems that my use case isn't really covered in their intended use cases.
Any way I can achieve my desired usage of Gradle, or am I stuck with exporting test-only code in the main JAR file of A? All help much appreciated.
I would treat code that is only needed during testing exactly the same as libraries that are only used during testing (e.g. JUnit or Mockito): The code should be in a separate module with its own name.
So I suggest that you split A into two modules (of the same multi-project build):
A
A-test-support
The dependencies of A would have to change to:
dependencies {
testCompile project(':A-test-support')
}
In B you have to use these dependencies:
dependencies {
compile(group: 'com.example', name: 'A', version: '0.1')
testCompile(group: 'com.example', name: 'A-test-support', version: '0.1')
}
What would be the easiest way to tell Gradle the following:
Retrieve 'junit' dependency and take its latest 'release' version.
Managing Maven and Ivy repositories is sort of new to me. I tried the following steps and they result in Could not resolve dependency ... error:
Write compile "junit:junit:latest.release" with repositories set to only mavenCentral() (however, it works if I say "junit:junit:4.10").
Write compile "junit:junit:latest.release" with repository set the following way:
ivy {
// I also tried 'http://maven.org' and other possible variants.
url "http://repo1.maven.org"
layout "maven"
}
Attempted to use Spring Source Ivy repository:
ivy {
artifactPattern "http://repository.springsource.com/ivy/libraries/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
ivyPattern "http://repository.springsource.com/ivy/libraries/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
}
Maybe I misunderstand something. Why would getting the latest version of the dependency be such a hard task?
It can be quite useful sometimes to get the latest release - if for example you release often your own dependencies.
You can get the latest version like
compile "junit:junit:+"
or better specify at least the major version like
compile "junit:junit:4.+"
Gradle currently does not support Maven's RELEASE (which is rarely used and deprecated) but it does support Ivy's latest.release (and for snapshots latest.integration). However, the general recommendation is to build against exact versions. Otherwise, the build can become a lottery.
Check out the Gradle-Versions-Plugin. It does exactly what you want: https://github.com/ben-manes/gradle-versions-plugin
For the installation, see the github page. Basically you need to add these two lines to your build.gradle - project file:
apply plugin: 'com.github.ben-manes.versions'
buildscript {
[...]
dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.8'
[...]
}
}
[...]
Then you can use the plugin, by running this command in terminal in your project dir:
./gradlew dependencyUpdates -Drevision=release
And it will show you which dependencies are outdated!
Latest Gradle User Guide mentions and explains plus sign in versions:
From 7.2. Declaring your dependencies:
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
... The build script also states that any junit >= 4.0 is required to compile the project's tests.
From 23.7. How dependency resolution works:
If the dependency is declared as a dynamic version (like 1.+), Gradle will resolve this to the newest available static version (like 1.2) in the repository. For Maven repositories, this is done using the maven-metadata.xml file, while for Ivy repositories this is done by directory listing.
In Android Studio:
If you're using + for the version, and want to know which version is actually being used, select Project in the sidebar, and then under External Libraries you will see the actual version number in use.
Another similar notation for Kotlin DSL (build.gradle.kts):
dependencies {
implementation("or.jsoup", "jsoup") {
version {
require("1.14.+")
}
}
// OR simply
// implementation("or.jsoup:jsoup:1.14.+")
}
Read more about this in Gradle documentations.
An excerpt from the docs:
A dynamic version can be either a version range (e.g. 2.+) or it can be a placeholder for the latest version available e.g. latest.integration.