I am currently building a jar using Gradle. I also generate source and javadoc jars. So far everything is by the book.
Now I want to create an additional jar, that has different content (few classes ignored). This jar should be published using a classifier.
Snippets from build.gradle:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'osgi'
apply plugin: 'maven'
apply plugin: 'signing'
configurations {
published
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives jar
archives sourcesJar
archives javadocJar
published jar
published sourcesJar
published javadocJar
}
I am not able to create an additional jar that is going to have everything: manifest updated by OSGI plugin, POM file updated, and signed. I am able to produce an additional JAR, but that jar is just a simple archive, and not signed/updated by plugins etc.
How to create an additional jar that is going to be named with classifier but treated as the regular jar (signed, uploaded to maven central, etc...)?
My try:
configure (java8Modules()) {
configurations {
java8
}
task code8Jar(type: Jar, dependsOn: classes) {
classifier = 'java8'
from sourceSets.main.output.classesDirs
}
artifacts {
java8 code8Jar
published code8Jar
}
}
gives me a jar, but it is not signed, and manifest is not modified.
Related
Let's say I need a .jar file that I have locally published to my maven local repository. I do not control building the jar, but I just have the artifact itself (my-custom-thing.jar for example). I have a project with this structure:
build.gradle
settings.gradle
lib/my-custom-thing.jar
How would I configure the maven publish plugin to publish this jar to maven, with correct POM file attributes?
I have something like:
plugins {
id 'java'
id 'maven-publish'
}
def myJar = layout.projectDirectory.file('lib/my-custom-thing.jar')
publishing {
publications {
myPublication(MavenPublication) {
version = "5.3"
groupId = "com.me"
artifactId = "my-custom-jar"
artifact(myJar) {
classifier "src"
extension "zip"
}
}
}
}
group 'org.example'
version '1.0-SNAPSHOT'
but that doesn't seem to work. I've tried with various variations of some of the code there and none of it seems to publish the artifact. I see a pom file being generated that matches what I expect, so it's on the right track, but I can't seem to publish the jar itself to my maven local. Anyone have any ideas on how to do that?
Using gradle 5.4 with a project dependency from external folder in a netbeans project.
The external folder contains resources like images, xml and custom objects that can only be created by this netbeans project. These external assets are then used to create binary files that get packed into a separate jar by that netbeans project.
These same external resources are also used during runtime for development in the gradle project. While I need the resources for development in the gradle project, I do not need or want them to be included in any jars anywhere for any reason when using the task build command because only the binaries are needed for distribution.
How to exclude the external resources from any and all jar files in the gradle project but allow them to be used for the classPath so I can run the project?
Some code examples of failure.
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
sourceSets {
main {
resources {
srcDir '../ExternalResourceFolder/assets'
}
}
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
dependencies {
runtimeOnly files('../ExternalResourceFolder/assets')
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
distributions {
main {
contents {
exclude '../ExternalResourceFolder/assets/**'
}
}
}
Tried many more things like adding to classPath and exclude but it would just be clutter to add them. Changing from sourceSet to dependency only moves the problem around from "build/lib" folder to "build/distributions" folder.
Had to exclude per file type in the end.
sourceSets {
main {
resources {
srcDir '.'
exclude ('**/*.j3odata','**/*.mesh','**/*.skeleton',\
'**/*.mesh.xml','**/*.skeleton.xml','**/*.scene',\
'**/*.material','**/*.obj','**/*.mtl','**/*.3ds',\
'**/*.dae','**/*.blend','**/*.blend*[0-9]','**/*.bin',\
'**/*.gltf')
}
}
}
I have an Android closed source module that will be be used as a library in other projects. It contains external dependencies.
To publish it, I'm creating a Maven artifact with the following gradle task:
apply plugin: 'maven'
def coreAarFile = file('...build\\outputs\\aar\\android-sdk-release.aar')
artifacts {
archives coreAarFile
}
uploadArchives {
repositories.mavenDeployer {
repository(url: "file://.\\mvn-repo")
pom.project {
groupId 'a.blabla'
artifactId 'blabla-sdk'
version "1.0.0"
}
}
}
It generates the .aar file, the pom.xml, etc without problems.
Then I create a project that have a dependeny to my library declared. It works until it needs to access to the external dependencies, when throws a class not found exception.
How can I edit my gradle task to include external dependencies or at least a reference to them? They are published in mvnrepository.com and github.com.
I moved uploadArchives to the build.gradle of the module and removed the artifacts element. It works!
Thanks to CommonsWare for pointing to the right direction.
I am new to Gradle. I would like to manipulate the following build.gradle contents to do this. Instead of separately running the tests then building the jar via separate commands, I'd like to do both in one command, except that the jar does not get created if one of the tests fail (it will not even try to build the jar).
apply plugin: 'java'
apply plugin: 'eclipse'
version = '1.0'
sourceCompatibility = 1.6
targetCompatibility = 1.6
// Create a single Jar with all dependencies
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.axa.openam'
}
baseName = project.name
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
// Get dependencies from Maven central repository
repositories {
mavenCentral()
}
test {
testLogging {
showStandardStreams = true
}
}
// Project dependencies
dependencies {
compile 'com.google.code.gson:gson:2.5'
testCompile 'junit:junit:4.12'
}
Thanks!
The simplest solution is to place all the tasks you want gradle to execute in order. So you may use the following:
gradle clean test jar
Tasks Breakout
clean: this is used mainly just to safely remove the last outdated jar (this is not mandatory);
test: execute the tests;
jar: create the jar artifact.
Key point: if one of the task fails for some reason gradle stops its execution.
So if just a single test fails for some reason an exception is thrown and the jar file is not created at all.
Alternative solution: add 'test' as dependency of 'jar'
Just to explore some other possibilities: modify the build.gralde file as follows:
[...]
jar {
dependsOn 'test'
[...]
}
[...]
Now every time you run gradle jar the test task is automatically executed before.
Emulate the pure command line solution using 'dependsOn'
To emulate the first command line approach (i.e., gradle clean test jar) using the dependency method you have to further modify the build.gradle. This is because is not assured that multiple dependsOn statements are evaluated in order:
[...]
jar {
dependsOn 'clean'
dependsOn 'test'
tasks.findByName('test').mustRunAfter 'clean'
[...]
}
[...]
Now you can use:
gradle jar
and both the tasks clean and test are executed (in the right order) before the actual jar task.
I'm making use of the Gradle maven plugin to build an artefact that is to be used for another unrelated project. Along with the built .jar artefact, I would also like to generate and install the -javadoc.jar artefact along side it.
Using gradle clean build javadoc install generates the JavaDoc in the local build file, and install the built artefact to the local repository, but it currently does not build and install -javadoc.jar along side it.
Is there a way to do this in Gradle using the maven or javadoc plugin? I don't mind writing a custom task to do it, but I rather use the "officially supported" way if one exists.
The build.gradle file:
project.group = "org.example.artefact"
project.version = "1.0-SNAPSHOT"
apply plugin: 'java'
apply plugin: 'maven'
dependencies {
// ...
}
uploadArchives {
repositories {
mavenDeployer {
// Custom repository location
repository(url: "file:///home/user/.m3/repository")
}
}
}
Javadocs are produced by the javadoc task (I think you are mistakenly referring to it as a plugin). This task isn't actually executed by default when running build or install. Additionally you'll want to define a jar task to bundle the javadocs and tell your build to publish that artifact by adding it to the artifacts {...} block.
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
artifacts {
archives javadocJar
}
Running install should then both create the javadoc jar and publish it to maven local. Additionally, running uploadArchives would then publish that artifact to any configured repositories.
Edit: Updated to add definition of javadoc jar task.