I created a gradle plugin (short named as testinfra), which has compile time and runtime dependency on 2 other jars. Here is my build.gradle for my plugin:
/**
* Define the dependencies for compiling the plugin code
*/
dependencies {
compile gradleApi()
compile localGroovy()
compile group:'com.mycompany.tools', name: 'lrgmanager', version: '1.0+'
compile group: 'com.mycompany.tools', name: 'LrgParser', version: '1.0+'
}
/**
* This task is responsible for publishing the plugin jar in the artifactory
*/
group = 'com.mycompany.tools'
version = '1.0'
publishing {
publications {
publishPlugin(MavenPublication) {
artifact jar
}
}
}
I am able to push my plugin jar to artifactory. But when I want to use the plugin, I have to specify the GAV of my plugin and also its dependencies. See below:
/**
* define classpath for buildscript{}, to download the regression plugin and its dependencies
*/
buildscript {
dependencies {
classpath group: 'com.mycompany.tools', name: 'testinfra', version: '1.0+'
classpath group:'com.mycompany.tools', name: 'lrgmanager', version: '1.0+'
classpath group: 'com.mycompany.tools', name: 'LrgParser', version: '1.0+'
}
}
apply plugin: 'testinfra'
My requirement is to avoid specifying the dependent jar's GAV along with the testinfra plugin GAV. Is there a way where I can configure my pom.xml (publish task) during publish such that when I specify GAV of my plugin, gradle identifies the dependencies and downloads them as well ?
I tried to reference the gradle user guide for this but I don't see any clear example for this.
Instead of artifact jar, use from components.java. Then the dependencies should appear in the POM, and Gradle will pull them in automatically.
Related
I'm converting a Java project from Maven to Gradle.
One of the pom.xml is using maven-assembly-plugin to package dependencies into a single zip file.
I'm using custom configuration to specify the dependencies to package like so:
group = 'com.company'
description = 'projectA'
configurations {
ciPlugin
}
dependencies {
ciPlugin group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.3.1'
}
jar {
into('plugins') {
from configurations.ciPlugin
}
}
This works almost fine beside the fact I have the following files in the resulting zip file:
commons-codec-1.10.jar
commons-logging-1.2.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
and when Maven runs it only has httpclient-4.5.3.jar.
Just for clarification, I'm not really packaging httpclient, I'm packaging private artifact but the behavior is the same.
How can I get only the direct dependency without transient ones?
Add the following (e.g. after the dependencies block):
configurations.ciPlugin.transitive = false
This will turn off transitive dependencies for all artifacts in the ciPlugin configuration.
I need a dependency which has an inconsistent version number in it's pom.
Apache XmlSchema-Pom has as version SNAPSHOT which is obviously not correct as it should be 1.1.
According to this gradle discussion it should be possible if the maven repository specified as an ivy repo, adding #jar or transitive = false to the dependency, all that didn't work for me
Here my build.gradle with my attempts:
group 'de.company'
version '1.0-SNAPSHOT'
apply plugin: 'maven'
apply plugin: 'java'
repositories {
// specified as ivy repo
// ivy {
// url = mavenCentral().url
// }
mavenCentral()
}
dependencies {
// with #jar and transitive
// compile (group: 'org.apache.ws.commons', name: 'XmlSchema', version: '1.1', ext: 'jar') {
// transitive = false
// }
compile group: 'org.apache.ws.commons', name: 'XmlSchema', version: '1.1'
}
Here is the error message which gradle outputs:
Could not resolve all dependencies for configuration ':compileClasspath'.
> Could not resolve org.apache.ws.commons:XmlSchema:1.1.
Required by:
de.company:gradle-test:1.0-SNAPSHOT
> Could not resolve org.apache.ws.commons:XmlSchema:1.1.
> inconsistent module metadata found. Descriptor: org.apache.ws.commons:XmlSchema:SNAPSHOT Errors: bad version: expected='1.1' found='SNAPSHOT'
The way i solved this is different, I don't want to touch artifactory pom as i don't have access to artifactory. here is the code you need in gradle.build
repositories {
maven {
url 'http://xxxxx/xx'
metadataSources {
artifact() //Look directly for artifact
}
}
}
As to the current date, there is no actual way of ignoring the validating of the poms from gradle.
Still there are some ways to workaround that.
Try use an other version of that dependency, where the pom is valid
Check other repositories, maybe they have an valid pom for the depedency you want.
that would be in my example for XmlSchema the jcenter repository (XmlSchema from jcenter)
Download the sources by yourself and deploy it into your local/company repository and use this version instead
In build.gradle we specify the dependencies as
compile group: 'org.apache.pig', name: 'pigunit', version: '0.11.0-cdh4.5.0', transitive: true
Running gradle cleanEclipse eclipse sets up the projects(adds the jar to classpath)
However there are only maven dependencies available for some APIs
(I am trying to run jersey 2.x examples bundle from https://jersey.java.net/download.html and it provides only pom.xml)
EDIT:
I know i can specify
compile group: 'groupId', name: 'artifactId', version: 'version' gradle but doing it manually for all dependencies or writing a program to do so should not be natural gradle way.
Gradle provides a maven plugin http://gradle.org/docs/current/userguide/maven_plugin.html.I haven't tried it out but it should be able to do it
Gradle supports Maven dependencies. Just specify the dependencies in the same way as your example:
compile group: 'groupId', name: 'artifactId', version: 'version'
To lookup the the artifact coordinates, you can use sites like http://search.maven.org
The only thing you have to make sure is to include either your internal Maven repository (if you are in a company which has one) or Maven Central:
repositories {
mavenCentral()
}
or
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}
I am new to Gradle so any help with this error will be highly appreciated.
I am building a REST based service using Spring-boot. I want to publish the JAR file to the local maven repository so that my web application can use it. After trying many things, I finally settled for maven-publish plugin. Here is my build.gradle file
//Needed for spring-boot
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE")
}
}
apply plugin: 'eclipse'
// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'
//apply Spring-boot plugin
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'
// In this section you declare where to find the dependencies of your project
repositories {
mavenLocal()
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
group = "com.proto"
publishing {
publications {
maven(MavenPublication) {
groupId "${project.group}"
artifactId "${project.name}"
version "${project.jar.version}"
artifact sourceJar { classifier "sources" }
from components.java
pom.withXml {
asNode().appendNode('parent')
.appendNode('groupId', 'org.springframework.boot').parent()
.appendNode('artifactId', 'spring-boot-starter-parent').parent()
.appendNode('version', '1.1.8.RELEASE')
asNode().appendNode('repositories').appendNode('repository')
.appendNode('id', 'spring-releases').parent()
.appendNode('url', 'http://repo.spring.io/libs-release')
}
}
}
}
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
jar {
baseName = 'my-api'
version = '0.0.1'
}
task('execJar', type:Jar, dependsOn: 'jar') {
baseName = 'my-api'
version = '0.0.1'
classifier = 'exec'
from sourceSets.main.output
}
bootRepackage {
withJarTask = tasks['execJar']
}
// In this section you declare the dependencies for your production and test code
dependencies {
// We use the latest groovy 2.x version for building this library
compile 'org.codehaus.groovy:groovy-all:2.3.6'
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web")
// {
// exclude module: "spring-boot-starter-tomcat"
// }
// compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// We use the awesome Spock testing and specification framework
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
testCompile 'junit:junit:4.11'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('cglib:cglib:3.1')
}
// tag::wrapper[]
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
}
My problem is that, when I run:
gradle publishToMavenLocal
I get the following error:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':publishMavenPublicationToMavenLocal'.
> Failed to publish publication 'maven' to repository 'MavenLocal'
> Unable to initialize POM pom-default.xml: Cannot find parent: org.springframework.boot:spring-boot-starter-parent for project: com.proto:proto-api:jar:0.0.1 for project com.proto:proto-api:jar:0.0.1
My gradle environment details:
------------------------------------------------------------
Gradle 2.1
------------------------------------------------------------
Build time: 2014-09-08 10:40:39 UTC
Build number: none
Revision: e6cf70745ac11fa943e19294d19a2c527a669a53
Groovy: 2.3.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_72 (Oracle Corporation 24.72-b04)
OS: Linux 3.13.0-39-generic amd64
What am I missing?
Thank you in advance for your help.
Ok, I have fixed the issue.
I am behind our corporate firewall, and had configured proxy correctly for gradle in ~/.gradle/gradle.properties file. But, I missed setting proxies for maven in ~/.m2/settings.xml file.
I configured our internal nexus repository to handle this issue but setting proxies block should work as well. Click here for maven settings.xml documentation
Same as #aardee, I am sitting behind our corporate firewall but it seems that my proxy settings (settings.xml) for maven local did not change anything. Fortunately we have our own maven repository that can proxy out and so I just replaced the repository in the generated pom and made sure that our company maven repository knows the relevant spring repos.
pom.withXml {
asNode().appendNode('parent')
.appendNode('groupId', 'org.springframework.boot').parent()
.appendNode('artifactId', 'spring-boot-starter-parent').parent()
.appendNode('version', '1.1.8.RELEASE')
asNode().appendNode('repositories').appendNode('repository')
.appendNode('id', 'spring-releases').parent()
.appendNode('url', 'http://my.mavenRepo.com/releases}
Replace http://my.mavenRepo.com/releases with your own maven repository.
My build failed due to this error:
A problem occurred evaluating project ':DBSupport'. > Could not find
method providedCompile() for arguments [project ':Core:Platform '] on
project ':DBSupport'.
Any idea what that means?
description = 'DBSupport main component of DBSupportTool'
dependencies {
providedCompile project(':Core:Platform')
providedCompile project(':Core:Verification')
providedCompile project(':DBSupportWeb')
providedCompile project(':DBSupportEJB')
compile(group: 'commons-lang', name: 'commons-lang', version:'1.0.1') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
compile(group: 'commons-logging', name: 'commons-logging', version:'1.0.4') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
compile(group: 'javax', name: 'j2ee', version:'1.0') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Please review and delete this closure when resolved. */
}
I assume these modules should in fact be treated as provided (e.g. should not be packages in a WAR archive). If not just change it to compile.
providedCompile configuration is not available in Gradle out of the box. If this is a web module you can just add/apply a war plugin:
apply plugin: 'war'
If not you should be able to add this configuration manually:
configurations {
providedCompile
}
dependencies {
providedCompile project(':Core:Platform')
...
}
sourceSets.main.compileClasspath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile
There is also a propdeps-plugin which claims to do the same thing transparently.
To define a dependency provided as in Maven you need to go the following way:
project(':webgui') {
apply plugin: 'war'
dependencies {
compile project (':domain')
providedCompile 'javax:javaee-api:6.0'
}
}
or an other way would be in case of a project (module) like this:
dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0#jar"
dependency ":providedCompile-transitive:1.0#jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0#jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0#jar"
testCompile "junit:junit:4.11"
moreLibs ":otherLib:1.0"
}