Gradle include transitive dependencies in directory - java

I have a java project which has a couple of dependencies, e.g.:
dependencies {
compile("com.whatever.jar:jar-1:1.2.3")
compile("com.whatever.jar:jar-2:4.5.6")
compile("com.whatever.jar:jar-3:7.8.9")
}
I package it as a jar library and publish it to the repository together with a POM file that describes those dependencies:
<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.my.project</groupId>
<artifactId>my-library</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-1</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-2</artifactId>
<version>4.5.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.whatever.jar</groupId>
<artifactId>jar-3</artifactId>
<version>7.8.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
I also have another project which depends on this previously built jar on compile time:
dependencies {
compileOnly("com.my.project:my-library:1.0.0")
}
I would like to create a task that builds the second project and creates a zip file containing both com.my.project:my-library:1.0.0 jar and its transitive dependencies (jar-1, jar-2 and jar-3).
task createZip << {
into('lib') {
// here I would like to do something like (it's a horrible pseudo-code):
from configurations.(com.my.project:my-library:1.0.0).jar.transitiveDependencies.files
}
}
How do I achieve that?
Edit: I also would like to access the list of my-library's transitive dependencies (and precisely those, not any other dependencies in the project) programmatically in order to build a Loader-Path manifest attribute using those.

Here’s a self-contained build.gradle script which demonstrates how your createZip task (which only adds specific dependencies to the ZIP archive) can be created:
/* make the "compileOnly" configuration available */
plugins {
id 'java'
}
/* add some demo dependencies to the project */
repositories {
jcenter()
}
dependencies {
compileOnly 'junit:junit:4.12'
compileOnly 'org.slf4j:slf4j-simple:1.7.28'
}
task createZip(type: Zip) {
// the "lib" directory in the ZIP file will only have all those dependency
// files of the "compileOnly" configuration which belong to the
// "junit:junit:4.12" module:
def depOfInterest = dependencies.create('junit:junit:4.12')
into('lib') {
from configurations.compileOnly.resolvedConfiguration.getFiles({dep ->
dep == depOfInterest
})
}
// the "justForDemonstration" directory in the ZIP file will have all
// dependency files of the "compileOnly" configuration, incl. SLF4J:
into('justForDemonstration') {
from configurations.compileOnly
}
}
This can be run with ./gradlew createZip (tested with Gradle 5.6.2). It produces the ZIP file under build/distributions/<your_project_name>.zip. Let me know if that’s what you had in mind with your pseudocode.

task uberJar(type: Jar) {
dependsOn classes
from sourceSets.main.runtimeClasspath.collect { it.directory?fileTree(it):zipTree(it)}
classifier = 'uber-jar'
}
assemble.dependsOn uberJar

Related

Dependency can't be found if gradle version is increased

We currently use gradle 5.6.4 version. After I upgrade the gradle version, above 6, I tried with 6.3 and 6.7.1, the following error occurs:
What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find com.lowagie:itext:2.1.7.js8.
Required by:
project : > net.sf.jasperreports:jasperreports:6.16.0
The dependency exists in ".gradle/caches/modules-2/files-2.1". I also tried with --refresh-dependencies flag, but nothing works. Any ideas?
I had the same issue. Some solutions which worked for me:
Adding new repositories
repositories {
mavenCentral()
maven{url "http://jasperreports.sourceforge.net/maven2/"}
maven{url "http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/"
}
dependencies {
...
implementation 'net.sf.jasperreports:jasperreports:6.16.0'
...
}
Excluding a deprecated artifact (com.lowagie)
<dependency>
<groupId>jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.16.0</version>
<exclusions>
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
</exclusions>
</dependency>
PS. Similar problem with itext library
I got the following build error:
Could not find com.lowagie:itext:2.1.7.js6
So I added http://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/ as repository and now it works.
repositories {
mavenCentral()
maven { url "https://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/" }
}
dependencies {
compile 'net.sf.jasperreports:jasperreports:6.8.0'
}
I had the same issue. I externally added the dependency for itext 2.1.7, which required for the complier.
implementation 'com.lowagie:itext:2.1.7'
implementation 'net.sf.jasperreports:jasperreports:6.17.0'

Maven: How to use a build.gradle file?

I'm a complete beginner with both Maven and Gradle. I have a working Maven project. Now I want to add a bunch of classes from another project which was built using Gradle. I want to take classes from the Gradle project and use it in my Maven project. When I copy the classes into my Maven project, I get a bunch of dependency errors. To resolve those errors I would like to add the required stuff to my pom.xml file.
Basically, I want to add all dependencies specified in a build.gradle file, to my pom.xml file.
The pom.xml file:
<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>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-core -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-jdk1.5 -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-jdk1.5</artifactId>
<version>0.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-ext -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-ext</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0-jre</version>
<!-- or, for Android: -->
<!-- <version>27.0-android</version> -->
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.trove4j/trove4j -->
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</project>
The build.gradle file:
buildscript {
repositories {
jcenter()
maven { url 'https://maven.rapidminer.com/content/groups/public/' }
}
}
plugins { id 'com.rapidminer.extension' version '0.8.2' }
// Define Maven artifact repositories
repositories {
jcenter()
maven { url 'https://maven.rapidminer.com/content/groups/public/' }
ivy {
url "https://github.com/rapidprom/rapidprom-libraries/raw/${project.properties["version"]}/prom/"
layout "pattern", {
artifact "[module]-[revision]/[module]-[revision].[ext]"
artifact "[module]-[revision]/[artifact].[ext]"
ivy "[module]-[revision]/ivy-[module]-[revision].xml"
}
}
ivy {
url "https://github.com/rapidprom/rapidprom-libraries/raw/${project.properties["version"]}/thirdparty/lib/"
layout "pattern", {
artifact "[module]-[revision]/[module]-[revision].[ext]"
ivy "[module]-[revision]/ivy-[module]-[revision].xml"
}
}
ivy {
url "https://github.com/rapidprom/rapidprom-libraries/raw/${project.properties["version"]}/thirdparty/resource/"
layout "pattern", {
artifact "[module]-[revision]/[module]-[revision].[ext]"
ivy "[module]-[revision]/ivy-[module]-[revision].xml"
}
}
}
extensionConfig {
// The extension name
name 'RapidProM'
/*
* The artifact group which will be used when publishing the extensions Jar
* and for package customization when initializing the project repository.
*
*/
groupId = 'org.rapidprom'
/*
* The extension vendor which will be displayed in the extensions about box
* and for customizing the license headers when initializing the project repository.
*
*/
vendor = "Eindhoven University of Technology"
/*
* The vendor homepage which will be displayed in the extensions about box
* and for customizing the license headers when initializing the project repository.
*
*/
homepage = "www.rapidprom.org"
// enable shadowJar before rapidminer dependency (otherwise build fails)
shadowJar {
zip64 true
}
// define RapidMiner version and extension dependencies
dependencies {
rapidminer '7.2.0'
//extension namespace: 'text', version: '7.2.0'
}
}
// Define third party library dependencies
dependencies {
compile group:"org.rapidprom", name:"ProM-Framework", version:"31245"
compile "org.rapidprom:AcceptingPetriNet:6.7.93"
compile "org.rapidprom:AlphaMiner:6.5.47"
compile "org.rapidprom:Animation:6.5.50"
compile "org.rapidprom:ApacheUtils:6.7.88"
compile "org.rapidprom:BasicUtils:6.7.104"
compile "org.rapidprom:BPMN:6.5.56"
compile "org.rapidprom:BPMNConversions:6.5.48"
compile "org.rapidprom:CPNet:6.5.84"
compile "org.rapidprom:DataAwareReplayer:6.7.563"
compile "org.rapidprom:DataPetriNets:6.5.291"
compile "org.rapidprom:DottedChart:6.5.17"
compile "org.rapidprom:EvolutionaryTreeMiner:6.7.154"
compile "org.rapidprom:EventStream:6.5.72"
compile "org.rapidprom:FeaturePrediction:6.5.61"
compile "org.rapidprom:Fuzzy:6.5.33"
compile "org.rapidprom:GraphViz:6.7.185"
compile "org.rapidprom:GuideTreeMiner:6.5.18"
compile "org.rapidprom:HeuristicsMiner:6.5.49"
compile "org.rapidprom:HybridILPMiner:6.7.116"
compile "org.rapidprom:InductiveMiner:6.7.297"
compile "org.rapidprom:InductiveVisualMiner:6.7.413"
compile "org.rapidprom:Log:6.7.293"
compile "org.rapidprom:LogDialog:6.5.42"
compile "org.rapidprom:LogProjection:6.5.38"
compile "org.rapidprom:ModelRepair:6.5.12"
compile "org.rapidprom:Murata:6.5.54"
compile "org.rapidprom:PetriNets:6.7.95"
compile "org.rapidprom:PNetAlignmentAnalysis:6.5.35"
compile "org.rapidprom:PNAnalysis:6.7.122"
compile "org.rapidprom:PNetReplayer:6.7.99"
compile "org.rapidprom:PTConversions:6.5.3"
compile "org.rapidprom:PomPomView:6.5.36"
compile "org.rapidprom:SocialNetwork:6.5.35"
compile "org.rapidprom:StreamAlphaMiner:6.5.15"
compile "org.rapidprom:StreamAnalysis:6.5.38"
compile "org.rapidprom:StreamInductiveMiner:6.5.42"
compile "org.rapidprom:TransitionSystems:6.7.71"
compile "org.rapidprom:TSPetriNet:6.5.35"
compile "org.rapidprom:Uma:6.5.46"
compile "org.rapidprom:Woflan:6.7.59"
compile "org.rapidprom:XESLite:6.7.217"
compile "org.rapidprom:Weka:6.7.3"
}
To deploy to Maven, use the maven plugin.
apply plugin: 'maven'
group = 'com.company'
version = '1.0.0.6'
// To build and push development snapshots, add a suffix to the name
// version = '1.0.0.6-SNAPSHOT'
artifacts {
archives jar
}
uploadArchives {
repositories {
mavenDeployer {
snapshotRepository(url: 'https://maven.repo/bla') {
authentication(userName: snapshotUser, password: snapshotPassword)
}
repository(url: 'https://maven.repo/bla') {
authentication(userName: releaseUser, password: releasePassword);
}
}
}
}
You can both install to a remote repository by running the uploadArchives task, (example above is https://maven.repo/bla), or install to ~/.m2/repository which is maven local using the install task.
gradle install uploadArchives
If you choose to install to maven local, you can add a dependency on maven local to other gradle projects by using dependencies { mavenLocal() }, or including it in your pom.xml
<settings <!-- bla --> >
<localRepository>${user.home}/.m2/repository</localRepository>
</settings>

maven external jar dependency

I am trying to use a jar given by my teacher in my code. So I placed the jar in a lib directory in my project and ran this command :
mvn install:install-file -Dfile=lib/IDLogger.jar -DgroupId=IDLogger -DartifactId=IDLogger -Dversion=1.0 -Dpackaging=jar
Everything is fine.
Then I add the dependency to the pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
But then when I try to use the jar, the compilation fails and tells me that the symbol(the class) can't be found. I even tried adding an import statement :
import IDLogger.IDLogger;
but it tells me that there is no such package.
How can I use this jar in my code in maven?
This is the code :
IDLogger logger = IDLogger.getInstance();
...
logger.logID(id);
I get the symbol IDLogger not found error
This is as much as I know about this custom jar...
Try to add your lib in system scope :
1. add in your project :
Your Project/lib
+IDLogger.jar
2. in your pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/IDLogger.jar</systemPath>
</dependency>

could not find or load main class org.tesng.TestNG

I am using testng-6.8.21.jar for writing test case from the following link:
http://www.tutorialspoint.com/testng/testng_tutorial.pdf
I am able to compile the java file TestNGSimpleTest.java
but when I try to use this command:
java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
it says:
could not find or load main class org.tesng.TestNG
You must provide full path to the jars in the classpath. For example :
java -cp '/path/to/testng-6.8.8.jar' org.testng.TestNG testng.xml
But testng requires other dependencies that you also must include in the classpath :
\- org.testng:testng:jar:6.1.1:test
+- junit:junit:jar:3.8.1:test
+- org.beanshell:bsh:jar:2.0b4:test
+- com.beust:jcommander:jar:1.12:test
\- org.yaml:snakeyaml:jar:1.6:test
The easiest way is to use a dependency manager. For example, Maven.
Briefly, you need (not required but it makes everything easier) to have a standard project structure :
main-directory
pom.xml <- File required by maven. It always has this name.
-src
-main
-java <- Place your Java classes here
-resources <- Place your images, conf files here etc.
-test
-java <- Place your java test classes here
-resources <- Place your test resources here.
Then, with this simple pom.xml, Maven understand that you want testng and downloads testNG's dependencies :
<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.mycompany</groupId>
<artifactId>my-app-name</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- Declare your dependencies here-->
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Then launch :
mvn test
And if you want to have a view on the dependencies, use :
mvn dependency:tree
(This is how I got the preceding dependency tree)

Gradle mavenDeployer - how to include a separate modules code

I have a project like this:
MyProject
|-ModuleA
|-ModuleB
Module A is an Android Library that creates an aar, it has a dependency on Module B like so:
dependencies {
compile project(':ModuleB')
In ModuleA I am using mavenDepoyer to release locally:
uploadArchives {
repositories.mavenDeployer {
pom.groupId = "com.foo"
pom.artifactId = "bar"
pom.version = "1.0"
repository(url: "file://${localReleaseDest}")
}
}
This generates me an AAR file and a POM.
When uncompressed the AAR does not contain the class files from Module B
and the POM looks like this:
<?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>com.foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>MyProject</groupId>
<artifactId>ModuleB</artifactId>
<version>unspecified</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
As you can see this declares that the AAR has a dependency on ModuleB with an unspecified version. And so if I use this this AAR/POM as a remote, it fails to resolve the dependency ModuleB.
Error:A problem occurred configuring project ':example'.
Could not resolve all dependencies for configuration ':example:_debugCompile'.
Could not find MyProject:ModuleB:unspecified.
Searched in the following locations:
https://jcenter.bintray.com/MyProject/ModuleB/unspecified/ModuleB-unspecified.pom
https://jcenter.bintray.com/MyProject/ModuleB/unspecified/ModuleB-unspecified.jar
Required by:
Test:example:unspecified > com.foo:MyProject:1.0
I do not want it to try and resolve Module B as another dependency, I want to use the mavenDeployer to be able to create the AAR & POM with Module B included inside, since I have the source code here to do that!
Searched the web to no avail, these sites gave hints but no answer:
How to publish apks to the Maven Central with gradle?
how to tell gradle to build and upload archives of dependent projects to local maven
http://www.gradle.org/docs/current/userguide/artifact_management.html
http://gradle.org/docs/current/userguide/userguide_single.html#sub:multiple_artifacts_per_project
http://gradle.org/docs/current/userguide/userguide_single.html#deployerConfig
As far as I know, AARs don't include their dependencies (only APKs do). Instead, transitive dependency resolution will take care of resolving not only the AAR but also its dependencies. The unspecified version is most likely a result of not setting the project.version property in ModuleB.
<dependency>
<groupId>MyProject</groupId>
<artifactId>ModuleB</artifactId>
<version>unspecified</version>
<scope>compile</scope>
</dependency>
The reason is your module dependencies is below :
compile project(':module B')
to resolve this issue, you should dependens maven dep
compile 'com.xxxxxx.xxx:xxxxx:1.0.0-SHNAPSHOT'

Categories