I have the following scenario:
I want to use a project of mine (hosted on bintray.com) in another project of mine.
I set up a maven repository, uploaded artifacts and pom files and then was able to utilize the jar file(s) uploaded to the bintray maven repo just fine, with the following build.gradle file:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'randers.test.usageTest.UsageTest'
repositories {
maven { url 'http://dl.bintray.com/randers00/NotEnoughVocab' }
jcenter()
}
dependencies {
compile(group: 'randers.notenoughvocab.core', name: 'notenoughvocab-core', version: '0.0.1', ext: 'jar')
}
jar {
manifest {
attributes "Main-Class": mainClassName
}
}
This build file successfully equips the project with my core library and even makes sources, etc. available in the IDE (IntelliJ IDEA I use)
The problem is: The core itself uses libraries, which are not gotten by gradle.
This is the pom file that is on bintray:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>randers.notenoughvocab.core</groupId>
<artifactId>notenoughvocab-core</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<licenses>
<license>
<name>GNU General Public License, Version 3.0</name>
<url>http://www.gnu.org/licenses/gpl.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<contributors>
<contributor>
<name>Ruben Anders</name>
<email>RAnders00#users.noreply.github.com</email>
<url>https://github.com/RAnders00</url>
</contributor>
</contributors>
</project>
I looked at other projects on bintray and their pom files look similar.
Declaring the dependecy the traditional and simple way works fine:
compile 'randers.notenoughvocab.core:notenoughvocab-core:0.0.1'
It doesn't work when you specify ext: 'jar', because that is used to download a single artifact. From the user guide:
Artifact only notation
As said above, if no module descriptor file can be found, Gradle by default downloads a jar with the name of the module. But sometimes, even if the repository contains module descriptors, you want to download only the artifact jar, without the dependencies. [14] And sometimes you want to download a zip from a repository, that does not have module descriptors. Gradle provides an artifact only notation for those use cases - simply prefix the extension that you want to be downloaded with '#' sign:
Example 50.5. Artifact only notation
build.gradle
dependencies {
runtime "org.groovy:groovy:2.2.0#jar"
runtime group: 'org.groovy', name: 'groovy', version: '2.2.0', ext: 'jar'
}
Related
Say I want to retrieve with graddle the dependancy tree of this artifact : com.google.firebase:firebase-firestore:24.4.0
How can I do ?
You can't do that, An aar does not contain any dependency information by itself.
All the information of this aar is stored in pom.xml which can be found here over google maven repo.
And this will only show you what Gradle dependencies command will do, and those are the transitive dependencies meaning the direct dependencies for this aar, Which By default, Gradle resolves them automatically.
the pom.xml for com.google.firebase:firebase-firestore:24.4.0
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-firestore</artifactId>
<version>24.4.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>androidx.annotation</groupId>
<artifactId>annotation</artifactId>
<version>1.1.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services-base</artifactId>
<version>18.0.1</version>
<scope>compile</scope>
<type>aar</type>
</dependency>
</dependencies>
<name>firebase-firestore</name>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
</project>
This pom.xml include com.google.android.gms which has its own pom.xml
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services-basement</artifactId>
<version>18.1.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>androidx.collection</groupId>
<artifactId>collection</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>androidx.core</groupId>
<artifactId>core</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
<type>aar</type>
</dependency>
<dependency>
<groupId>androidx.fragment</groupId>
<artifactId>fragment</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
<type>aar</type>
</dependency>
</dependencies>
<name>play-services-basement</name>
<licenses>
<license>
<name>Android Software Development Kit License</name>
<url>https://developer.android.com/studio/terms.html</url>
<distribution>repo</distribution>
</license>
</licenses>
</project>
What I am trying to say, Is that unless you iterate the process and fetch the POM files of the dependencies yourself, with a custom task, All you can use is gradle dependencies command to check the transitive dependencies used by your project or module.
UPDATE:
You can easily start a new gradle project by following these simple steps.
mkdir gradleExp
cd gradleExp
gradle init # 1.basic 1.groovy random name
update the empty build.gradle with the following
plugins {
id 'java'
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation "com.google.firebase:firebase-firestore:24.4.0"
}
gradle dependencies # to list all
gradle dependencies --configuration compileClasspath # reduce output to show only Compile classpath for source set 'main'
NOTE: missing either google() or mavenCentral() will show some failure in the result shown.
If it is a dependency of your project you should be able to run
gradle dependencies
and see the dependency tree for your whole project (including the subtree for this artifact)
There are more details in the answer(s) to this question:
Using Gradle to find dependency tree
I'm writing a library that I'd like to compile into implementable jar which then will be used in other projects / tests.
In my library I depend on various jars: okHttp, guava, etc., What I want to do is to tell maven not to put those dependencies into the final JAR but make that projects / modules that depend on this library provide those dependencies
How can this be done in maven?
library pom.xml
<groupId>com.example</groupId>
<artifactId>testing-library</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
implementation module pom.xml
<groupId>com.example</groupId>
<artifactId>implementation-</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>testing-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
But I'm getting java.lang.NoClassDefFoundError: com/google/common/base/Preconditions error
If you put code into src/test/java, this code will not be part of the final jar. The code is meant for tests during the build of the jar.
If your library is a helper library for tests, put the code into src/main/java and reference it in other projects with <scope>test</scope>.
BTW, don't use Maven shade plugin or Maven assembly plugin for a library. These are mainly meant for standalone jars that run on their own.
Ok, I solved the issue. It seems that the generated POM.xml for the testing-library did not contain any dependencies.
I was using mvn install:install-file ... -DgeneratePom=true for installing jar into local repository for quick debugging and the pom generated this way seemed to be lacking library dependencies'
I would like to use the Java AWS SDK only for S3 at the moment.
So, instead of importing the entire AWS SDK jar file I wanted to only import the necessary packages for accessing my S3 bucket into my web application (IDE: Netbeans).
To do this, I read that I should use Maven to build the jar I need.
I have tried two approaches but can't seem to also include all of the aws-java-sdk-s3 dependencies in the jar that I am building.
First approach
1) I download the zipped aws-sdk-java folder from https://github.com/aws/aws-sdk-java
2) I unzip to a local folder.
3) I navigate to the aws-java-sdk-s3 (ie: where the pom is located) folder in my console, then type "mvn clean install".
While this builds a jar file (located in aws-java-sdk-s3/target), the jar does not contain the dependencies specified in the POM file (eg: it does not include the BasicAWSCredentials class in the core package which I need).
Second Approach
Change the POM file located in the aws-java-sdk-master folder so that it imports the BOM and specifying my requirement (S3) by adding:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.10.67</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
</dependencies>
and removing all other dependencies listed in the POM (eg: junit).
This approach fails while building the DynamoDB package, which I don't even need it to be building (I want to specify that it only needs S3).
Any suggestions as to what I'm doing wrong?
Perhaps the main issue here is that I'm new to Maven.
Thank you
update your pom.xml properly likewise,
<dependencies>
....
<!-- AWS dependencies -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.10.43</version>
</dependency>
....
</dependencies>
You can download the Jar that you are interested directly from maven :
http://central.maven.org/maven2/com/amazonaws/aws-java-sdk-s3/1.10.66/aws-java-sdk-s3-1.10.66.jar
update the URL with the version of the sdk you want to use and you can just import the jar in your project, no need to rebuild it
It worked fine for me using Gradle. Here is the Gradle build file I used as advised by the API doc.
group 'aws.test'
version '1.0'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom 'software.amazon.awssdk:bom:2.0.0-preview-12'
}
}
dependencies {
compile 'software.amazon.awssdk:s3'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I want to try this library in my android project. I am using Android Studio 0.4.6.
The README.markdown file tells me to insert this inside pom.xml:
<!-- in the 'repositories' section -->
<repository>
<id>keytwo.net</id>
<name>Keytwo.net Repository</name>
<url>http://audiobox.keytwo.net</url>
</repository>
<!-- in the 'dependencies' section -->
<dependency>
<groupId>io.socket</groupId>
<artifactId>socket.io-client</artifactId>
<version>0.2.1</version> <!-- the desidered version -->
</dependency>
The problem is that I do not have any pom.xml. I created one in my project root directory and synced gradle settings but it does nothing. Till now I only used already compiled .jar files or used the gradle compile function.
How can I use this library in my project?
Android Studio doesn't use Maven as its builder; it uses Gradle instead. Fortunately, Gradle can use Maven repositories to fetch dependencies, so it's a matter of taking that information that would go into the pom file and using it in Gradle format. These modifications go in the build.gradle file in your module's directory (not the build file in the project root directory).
First, set up the repository where it can find the dependency.
repositories {
maven { url 'http://audiobox.keytwo.net' }
}
and then add the dependency itself by adding this line to your dependencies block:
dependencies {
...
compile 'io.socket:socket.io-client:0.2.1'
}
Update:
From POM file:
compile '<groupId>:<artifactId>:<version>'
Syntax:
implementation 'groupId:artifactId:version'
If this is what you have to import in your Android Studio Project...
// Maven : Add these dependecies to your pom.xml (java6+)
// <dependency>
// <groupId>org.glassfish.jersey.core</groupId>
// <artifactId>jersey-client</artifactId>
// <version>2.8</version>
// </dependency>
// <dependency>
// <groupId>org.glassfish.jersey.media</groupId>
// <artifactId>jersey-media-json-jackson</artifactId>
// <version>2.8</version>
// </dependency>
then it translates to this...
implementation 'org.glassfish.jersey.core:jersey-client:2.8'
implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:2.8'
While only for two days now, I am definitely sold on using gradle for all of my Java projects, and drop pom.xml from the root of all my projects.
However, I would like to remain maven-compatible, in the sense that I would like for a gradle task to be able to generate a suitable pom.xml at the root of the project should the user want it.
At this moment, the only reference to a pom.xml I have is in this section of the build.gradle file (this is, with very few modifications, what is found here):
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment {
MavenDeployment deployment -> signing.signPom(deployment);
}
repository(url: sonatypeRepoURI) {
authentication(userName: sonatypeUsername,
password: sonatypePassword);
}
pom.project {
name "${name}";
packaging "bundle";
description "${description}";
url "${projectURL}";
scm {
url "${gitroscm}";
connection "${gitroscm}";
developerConnection "${gitrwscm}";
}
licenses {
license {
name "Lesser General Public License, version 3 or greater";
url "http://www.gnu.org/licenses/lgpl.html";
distribution "repo";
}
}
developers {
developer {
id "whocares";
name "whocares";
email "whocares";
}
}
}
}
}
}
How would I extract the pom.project out of this very deeply nested construct into a task which could generate a pom.xml (by default, the generated pom.xml is in build/poms/pom-default.xml and looks quite good)?
More importantly, is it possible to extract that pom.project out of uploadArchives while still being able to refer to it?
Full link to the build.gradle file: here.
You can use the gradle maven plugin. This adds the pom convention method to your project, which you can use in a task to generate a pom.xml file, like
task writeNewPom {
doLast {
pom {
project {
groupId 'org.example'
artifactId 'test'
version '1.0.0'
inceptionYear '2008'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}.writeTo("pom.xml")
}
}
Then you call gradle createPom to generate the pom.xml in the project root. Of all the things in the pom definition, you should really provide groupId, artifactId and version, other thins like licenses are not that important.
You can also look at this example for a project definition with some dependencies, and try running it to see what it produces.
Some of the new keywords were added and some techniques were deprecated. Please check
Here is my build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-core:4.0.5.RELEASE'
compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'
compile 'org.slf4j:slf4j-api:1.7.5'
runtime 'org.slf4j:slf4j-log4j12:1.7.5'
testCompile 'org.springframework:spring-test:4.0.5.RELEASE'
testCompile 'junit:junit:4.11'
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.hamcrest:hamcrest-library:1.3"
testCompile 'javax.servlet:javax.servlet-api:3.0.1'
}
test {
testLogging {
// Show that tests are run in the command-line output
events 'started', 'passed'
}
}
task wrapper(type: Wrapper) { gradleVersion = '1.12' }
task createPom {
pom {
project {
groupId 'sg.test.spring.web.guide'
artifactId 'sg-web-initial'
version '1.0.0-SNAPSHOT'
inceptionYear '2008'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}.writeTo("pom.xml")
}
You can name the task createPom to anyTaskName as you like.
Then just run gradle clean or grale build or simply gradle createPom.
This will generate it as pom.xml in the root of the project. Although you can replace writeTo("pom.xml") with writeTo("<anyDir>/newpom.xml").
The resulting pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>sg.test.spring.web.guide</groupId>
<artifactId>sg-web-initial</artifactId>
<version>1.0.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Fast forward a few years and a few major Gradle releases, in Gradle 7.5.1 this is how one would approach this with the built-in publishing plugin (latest docs). This has the benefit over hand-writing the pom like above that it automatically gets a lot of the properties, dependencies from the normal Gradle setup:
plugins {
id("maven-publish")
}
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.named("generatePomFileForMavenPublication").configure {
def publication = publishing.publications.maven
destination = rootProject.file("${publication.artifactId}-${publication.version}.pom")
}
alternatively without mutating the built-in task we can copy it wherever needed:
tasks.register("copyPomToRoot") {
def publication = publishing.publications.maven
def generatePom = tasks.named("generatePomFileFor${publication.name.capitalize()}Publication")
dependsOn(generatePom)
def output = rootProject.file("${publication.artifactId}-${publication.version}.pom")
outputs.file(output)
doLast { output.bytes = generatePom.get().destination.bytes }
}
As a side-note: if you need a pom.xml, you probably don't just want it in your repo root, but want to publish to a proper maven repository: https://stackoverflow.com/a/70758552/253468.