I have a Java Gradle 2.14.1 project with a few subprojects: a, b, c and no. no is 'special' as it doesn't contain anything in src/main/java, it just has some e2e tests using all the modules. I want to apply the maven plugin to a, b and c, but not to no. Here is my code (in the form of a Bash session; please ignore the fact there is no code - it is the simplest thing which reproduces my issue):
maven-test $ find .
.
./a
./b
./build.gradle
./c
./gradle
./gradle/wrapper
./gradle/wrapper/gradle-wrapper.jar
./gradle/wrapper/gradle-wrapper.properties
./gradle.properties
./gradlew
./gradlew.bat
./no
./no/build.gradle
./settings.gradle
maven-test $ cat settings.gradle
rootProject.name = 'maven-test'
include ':a'
include ':b'
include ':c'
include ':no'
maven-test $ cat build.gradle
subprojects {
apply plugin: 'java'
if (it != project(':no')) {
println 'applying maven for ' + it
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://build")
}
}
}
} else {
println 'not applying maven for ' + it
}
defaultTasks = ['clean', 'build']
afterEvaluate { project ->
println("================ $project.path tasks:")
project.tasks.each { task ->
println(" $task.name")
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}
maven-test $ cat no/build.gradle
jar.enabled = false
I.e. the no/build.gradle file disables the jar task (as there is nothing to jar). The main build.gradle file configures the java and maven plugins, but the maven plugin is explicitly omitted from the no module. Also, there is some debugging output added to see what is going on.
Here is the output (a bit chatty, I hope it is Ok):
maven-test $ ./gradlew clean uploadArchives
applying maven for project ':a'
applying maven for project ':b'
applying maven for project ':c'
not applying maven for project ':no'
================ :a tasks:
assemble
build
buildDependents
buildNeeded
check
classes
clean
compileJava
compileTestJava
install
jar
javadoc
processResources
processTestResources
test
testClasses
uploadArchives
================ :b tasks:
assemble
build
buildDependents
buildNeeded
check
classes
clean
compileJava
compileTestJava
install
jar
javadoc
processResources
processTestResources
test
testClasses
uploadArchives
================ :c tasks:
assemble
build
buildDependents
buildNeeded
check
classes
clean
compileJava
compileTestJava
install
jar
javadoc
processResources
processTestResources
test
testClasses
uploadArchives
================ :no tasks:
assemble
build
buildDependents
buildNeeded
check
classes
clean
compileJava
compileTestJava
jar
javadoc
processResources
processTestResources
test
testClasses
:a:clean
:b:clean
:c:clean
:no:clean
:a:compileJava UP-TO-DATE
:a:processResources UP-TO-DATE
:a:classes UP-TO-DATE
:a:jar
:a:uploadArchives
:b:compileJava UP-TO-DATE
:b:processResources UP-TO-DATE
:b:classes UP-TO-DATE
:b:jar
:b:uploadArchives
:c:compileJava UP-TO-DATE
:c:processResources UP-TO-DATE
:c:classes UP-TO-DATE
:c:jar
:c:uploadArchives
:no:compileJava UP-TO-DATE
:no:processResources UP-TO-DATE
:no:classes UP-TO-DATE
:no:jar SKIPPED
:no:uploadArchives FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':no:uploadArchives'.
> Could not publish configuration 'archives'
> Cannot publish artifact 'no.jar (com.test:no:0.1.0-SNAPSHOT)' (/Users/wujek/Development/IdeaProjects/maven-test/no/build/libs/no-0.1.0-SNAPSHOT.jar) as it does not exist.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1.145 secs
What is interesting is: the output says the maven plugin is not configured for the no module (not applying maven for project :no), the uploadArchives task is not listed for the :no module, but in the Gradle output is still says this task is invoked and it fails. I can also invoke this 'phantom' task directly using ./gradlew :no:uploadArchives and it fails the same way instead of telling me the task is missing.
What is wrong with my configuration?
Related
I would like to build my project as a jar file which only contains my code. This project jar is used as a library to other java project.
Here is my build.gradle:
plugins {
id 'java'
}
group 'com.my.lib'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task sourcesJar(type: Jar) {
archiveClassifier = 'sources'
from sourceSets.main.allJava
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
After I build the project (I am using IntelliJ, I clicked "Build" ==> "Rebuild project"). IntelliJ told me build successfully But I don't see any jar created. Why?
18:30:18: Executing tasks ':classes :testClasses'...
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
18:30:18: Tasks execution finished ':classes :testClasses'.
I figured out that even I have those settings, when I choose Build->Build project, Intellij still not run Gradle build that's why in my original question there is no "sourceJar" showing. I have to open "Gradle tool" window, click on that little elephant icon to run gradle task. Then, type "Gradle build", it then run the source Jar task.
The "Build Project" form IntelliJ IDEA will compile your code for usage in the IDE, it is not directly tied to the build lifecycle task of Gradle.
For Gradle to build the JAR, you need to run ./gradlew assemble, which will have the expected outcome.
Note that this will not run tests or static analysis. If you want that to run as well, you can use ./gradlew build.
Both of these Gradle tasks can be executed from the Gradle tool window in IntelliJ IDEA.
Open the project in your file system and you might find it under the build/libs/.. folder
In my Gradle project, I define an additional sourceSet.
sourceSets {
sample {
java {
srcDir 'sample/java'
}
compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath
}
}
I then add a task:
task sampleJar(type: Jar) {
classifier 'sample'
from sourceSets.sample.output
}
artifacts {
archives sampleJar
}
If I do > gradle build the additional jar file builds from the additional source set. However, if I do > gradle jar, it doesn't. any reason why?
When I go through the output messages, I see:
gradle build has sampleJar in the Tasks to be executed:
but
gradle jar doesn't.
But unsure as to why?
Because jar is just the task that assembles the main jar file.
build, on the other hand, is the top-level life-cycle task, which depends on assemble. And assemble is documented as
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration.
Since your sampleJar pecisely creates an artifact attached to the archives configuration, assemble, and thus build depends on it.
I have a Kotlin project with JDA API which I need to deploy on Heroku environment. To achieve that I created a JAR task in my build.gradle file.
jar {
baseName = 'discord-stats-bot'
version = 'v1'
manifest {
attributes('Main-Class': 'com.vchernogorov.discordbot.BotKt')
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
But I was unable to build this JAR file because of the following error.
16:02:03 vchernogorov $ ./gradlew jar
:kaptGenerateStubsKotlin UP-TO-DATE
:kaptKotlin UP-TO-DATE
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:copyMainKotlinClasses UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:inspectClassesForKotlinIC UP-TO-DATE
:jar
FAILURE: Build failed with an exception.
* What went wrong:
Could not expand ZIP '/Users/vchernogorov/.gradle/caches/modules-2/files-2.1/club.minnced/opus-java/1.0.4/596995aaf2f5b5091c4d251fdc11fa62680cc59e/opus-java-1.0.4.pom'.
> archive is not a ZIP archive
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 6.784 secs
This library is a dependency in a JDA project. So I need help with configuring this jar task in order to correctly build my executable and deploy it to Heroku.
Edit: Here's my dependencies block in build.gradle.
dependencies {
compile 'com.google.guava:guava:23.0'
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16"
compile "org.jsoup:jsoup:1.10.3"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "net.dv8tion:JDA:3.8.0_436"
compile "khttp:khttp:0.1.0"
compile 'com.google.code.gson:gson:2.8.1'
}
In maven there's a special artifact of type pom. It is also published and downloaded as a dependency. In jar's from is neither a directory nor a jar file, that's why processing fails. To solve it you need to exclude the *.pom before collecting, so:
from {
configurations
.compile
.findAll { !it.name.endsWith('pom') }
.collect { it.isDirectory() ? it : zipTree(it) }
}
Next time you can use gradle shadow plugin or similar - a plugin that builds uber jar, since it probably handles it correctly.
We're using a gradle file to build a Java WAR file. I know very little about gradle. At the top of build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
We run the gradle with gradle clean install. I'm not sure where these tasks are defined but I assume they're in one of the plugins (I'd guess war).
When I run gradle clean install it seems to print the tasks that it are run:
:clean
:compileJava
:processResources
:classes
:war
:install
Correct me if I'm wrong, but it seems that the task install dependsOn compileJava, processResources, classes, and war.
I need a task I've written to run sometime after clean but sometime before war. Preferably without modifying the plugin.
I've tried indicating that my task mustRunAfter processResources but it doesn't work that way.
How can I inject my task as a dependency on install before the dependency war?
You can declare task dependencies explicitly.
Add following code to your build.gradle file
tasks.war.dependsOn("yourTaskNameHere")
tasks["yourTaskNameHere"].dependsOn("clean")
i thought uploadArchives is a task which is provided by the java plugin.
In my build.gradle i use the java plugin:
apply plugin: 'java'
But if i invoke gradle tasks on command line, i can't see the uploadArchives task.
Even not with gradle gradle tasks --all
The uploadArchives task is listed in the gradle java plugin documentation
see http://www.gradle.org/java_plugin (table 11).
I use gradle version 1.0-milestone-6.
I can invoke gradle uploadArchives without error, but the task is not listed.
The uploadArchives task is added as a Rule to your build script and not explicitly by name. In the output of "gradle tasks" you should see this line:
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
This means, that for each configuration in your build file, an according uploadTask exist. The java plugin adds an configuration named archives to your build script. By adding the configuration "archives" to your build script explicitly by the java plugin, the uploadArchives task is added implicitly too.
There are scenarios, where gradle can't know what tasks need to be materialized by a rule.
E.g.
tasks.addRule("Pattern: ping<ID>") { String taskName ->
if (taskName.startsWith("ping")) {
task(taskName) << {
println "Pinging: " + (taskName - 'ping')
}
}
}
There is no way to figure out which ping tasks should be shown as they are just materialized when triggered from commandline via 'gradle pingServer1 pingServer2 pingServer3'
regards,
René
The uploadArchives task is a part of the maven-plugin. You have to add:
apply plugin: 'maven'