Adding custom maven dependency to gradle project [duplicate] - java

This question already has answers here:
How to add a Maven project as a Gradle dependency?
(3 answers)
Closed 2 years ago.
I have created dependency jar using maven project but now i have to add this maven dependency into my gradle project.
Depencency available in my .m2 directory
Am getting the below error from intellij .
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Cannot convert URL 'com.example.auth.security:common:0.0.1-SNAPSHOT.jar' to a file.
Please find my build.gradle file
plugins {
id 'org.springframework.boot' version '2.1.16.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.example.auth'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation files('com.example.auth.security:common:0.0.1-SNAPSHOT.jar') --> getting error on this line.
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
implementation('io.jsonwebtoken:jjwt:0.9.1')
}
Update 1
A problem occurred evaluating root project 'auth-center'.
> Supplied String module notation '0.0.1-SNAPSHOT' is invalid. Example notations: 'org.gradle:gradle-core:2.2', 'org.mockito:mockito-core:1.9.5:javadoc'.

You will need to add a local maven repository like this
repositories {
maven { url new File(pathToYourM2Directory).toURI().toURL() }
}
Additionally the declaration of the dependency is not correct. It should be
dependencies {
implementation group: 'com.example.auth.security', name: 'common', version '0.0.1-SNAPSHOT'
}
You can as well fix the files dependency. However using a local maven repo is more sustainable as by resolving artifacts this way it is transparent for the build process if an artifact is resolved locally or remote.

Can't comment since I don't have sufficient reputation. I believe you shouldn't be trying to add the maven dependency to the gradle project. Instead, host the maven dependency elsewhere and configure gradle to pull dependencies from there. For reference, you can take a look at this answer
How to add a Maven project as a Gradle dependency?

This is another way to import your custom java library(.jar).
What you need to do is that first, make a folder wherever you want under your project, in my case /src/lib, and put JAR file into that folder. Then, write down the below code into your Gradle file.
dependencies {
//Library Auto Implement
//Replace with your folder URI
implementation(fileTree("./src/lib"))
}
Then Gradle will implement your JAR files from that folder, and you are ready to go.

Related

Issue while setting up the spring boot with gradle

I am trying to setting up a Spring boot gradle project in intelliJ. If I am trying with reload button on the gradle pane, it's giving me error for setting.gradle
but setting.gradle file is already there is the root directory.
When I tried ./gradlew clean build on terminal getting the below error:
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.0'
}
group 'com'
version '1.0'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
java {
sourceCompatibility = JavaVersion.toVersion('11')
targetCompatibility = JavaVersion.toVersion('11')
}
I tried but couldn't figure out what I am missing. Please suggest.
Update: I tried to delete gradle installation from the system and executed one another application which was working fine earlier but not even after downloading the gradle version, getting the same error as mentioned above.
I tried Maven project with spring initializr and it successfully build so It seems to be an issue regarding gradle setup on my machine but not sure what to check.**
Regards
Not Sure why error message was not that clear. It was related to certificate issue. I added some organization specific certificate in java cacerts keystore and it resolved the gradle issue with plugin download.
Thanks to All who tried to help.

Using Eclipse NonNull annotations with multiple Gradle projects

We've been using the Eclipse #NonNull and #Nullable annotations in our code for a while.
We are now adding some Spring projects defined with Gradle to our system. These projects will share quite a bit of code with our standalone projects as well.
We are seeing a problem due the Eclipse annotations (being compile time checking) don't work when a Spring/Gradle project refers to the shared code via Gradle generated .jar file. Eclipse needs to have the source of the shared jar in order for the annotations to work. Attaching the source in the Eclipse project only works until you need to do a Gradle Refresh, as that rebuilds the eclipse .project and .classpath files.
It's also a problem that you have to explicitly rebuild the shared .jar each time you make a change to the shared code. It's not done automatically.
I haven't found a way to have the Spring/Gradle projects just use a 2nd source directory for the shared code, and not need to have the shared code as a generated .jar file.
Is there any good way to have shared code between multiple Gradle projects in Eclipse - without using an intermediate .jar file? (Or some other way to get the Eclipse annotations to work.)
Not really sure how to give a full example, as most of this is gradle and eclipse configuration.
Here are the Gradle config files: settings.gradle
pluginManagement {
repositories {
maven { url 'https://repo.spring.io/milestone' }
gradlePluginPortal()
}
}
rootProject.name = 'App1-Account-Manager'
and build.gradle
plugins {
id 'org.springframework.boot' version '2.5.0-RC1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.efi'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.2'
implementation 'org.springframework.boot:spring-boot-starter-amqp'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
implementation files('lib/eflow/eFlowClientApi.jar',
'lib/eflow/eFlowCryptography.jar',
'lib/eflow/json-simple-1.1.1.jar',
'lib/eflow/commons-codec-1.4.jar',
'lib/eflow/commons-io-2.6.jar',
'lib/eflow/commons-lang-2.6.jar',
'lib/eflow/commons-logging-1.2.jar',
'../App1-Commons/build/libs/App1-Commons-0.0.1-SNAPSHOT.jar'
)
implementation group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '2.1.100'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.security:spring-security-test'
}
test {
useJUnitPlatform()
}
App1-Commons is the project of shared code that multiple other projects depend on. That's where a bunch of the #NonNull annotations are.
I can set the App1-Account-Manager project to depend on the App1-Commons project in eclipse - but as soon as you run a Gradle Refresh, it looses that connection.
I'd prefer to just have the source from App1-Commons included in the App1-Account-Manager project - but I don't see how to configure Gradle to do that with Eclipse projects.
I think I found the solution. I need to add a SourceSets block to the build.gradle file like this:
sourceSets {
main {
java {
srcDir 'src/main/java'
srcDir '../App1-Commons/src/main/java'
}
}
}
And remove the reference to the App1-Commons jar file.

Gradle Builds an Empty Jar File

I converted a project to gradle using gradle init from maven after updating the dependencies. gradle test works as expected. However, when I run gradle build, the jar file that's generated is entirely empty.
I've attempted to tweak the source set to make something happen, but that doesn't seem to solve the problem. The directory structure matches what gradle expects from what I can tell everything is nested in src/main/groovy
The project's full code is available on Github.
In general what causes no files to be added to a build? Is there additional configuration I need to add besides whatever gradle init creates?
Gradle build file:
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
implementation 'com.github.javafaker:javafaker:1.0.2'
testImplementation 'org.spockframework:spock-core:2.0-M3-groovy-3.0'
testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
}
group = 'nl.topicus.overheid'
version = '0.2.0'
description = 'java-factory-bot'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
What going on now is the project is try to build as Java project and get src from src/main/java as it is default from Java project. So, you need id 'groovy' not id 'java' in plugins section to make it look into src/main/groovy and build both .java and .groovy files in there.

Gradle unable to resolve Sonatype Nexus dependency in IntelliJ

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.

Maven dependency in a Gradle project

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

Categories