Gradle build doesn't generate jar - gradle

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

Related

Grails 4 - Gradle 'assemble' task not generating .war.original archive

I just upgraded my Grails web-app from version 3.2 to 4.0.
I have a provided dependency in build.gradle (fairly common configuration):
dependencies {
...
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-starter-tomcat"
...
}
I’ve just noticed that by executing Gradle assemble task I don’t get the myApp.war.original archive anymore (which used to be build without all provided libs), but only the one including provided dependencies.
Am I missing something here? I'd really like to spare those ~4MB of jars in lib-provided folder.
Thanks in advance!
Update 1
Following #ck1's advice i changed provided dependency to providedCompile, but the result is the same.
Though I already use the war plugin, I noticed that the sequence of tasks initiated by assemble task is:
> Task :assetCompile
Finished Precompiling Assets
> Task :compileJava
> Task :compileGroovy
> Task :buildProperties
> Task :processResources
> Task :classes
> Task :compileWebappGroovyPages NO-SOURCE
> Task :compileGroovyPages
> Task :compileGsonViews
> Task :findMainClass
> Task :bootWar
> Task :war SKIPPED
> Task :assemble
So the war task is skipped in favor of the new bootWar task (not available in Gradle 3, used by Grails 3).
Any way to force it? Or is it something the plugin should already support?
Update 2
After some research, I added to build.gradle
war {
enabled = true
}
and was able to get the war task to execute:
> Task :assetCompile
Finished Precompiling Assets
> Task :compileJava
> Task :compileGroovy
> Task :buildProperties
> Task :processResources
> Task :classes
> Task :compileWebappGroovyPages NO-SOURCE
> Task :compileGroovyPages
> Task :compileGsonViews
> Task :findMainClass
> Task :bootWar
> Task :war // not skipped
> Task :assemble
I basically got to where I wanted to, i.e. get a .war archive without all the provided dependencies; differently from before though, not a pair of .war archives (myApp.war and myApp.war.original) but a single one named myApp.war not including the unneeded stuff.
But I'm still pretty much confused, as
Spring Boot's Gradle plugin documentation states bootWar is an extension of war.
The bootRepackage task has been replaced with bootJar and bootWar tasks for building executable jars and wars respectively. Both tasks extend their equivalent standard Gradle jar or war task, giving you access to all of the usual configuration options and behaviour.
But then Spring Boot 2.0 Migration Guide states war task is expected to be skipped:
The bootRepackage task has been replaced with bootJar and bootWar tasks for building executable jars and wars respectively. The jar and war tasks are no longer involved.
So again, what am I missing out?
You should replace provided with either the providedCompile or providedRuntime dependency configuration from the war plugin.
These two configurations have the same scope as the respective compile
and runtime configurations, except that they are not added to the WAR
archive.
Reference:
https://docs.gradle.org/4.10.2/userguide/war_plugin.html

How can I automatically run google-java-format as part of my Gradle build?

Google-java-format-gradle-plugin integrates with Gradle, but how can I run it automatically as part of the normal build?
Sherter gradle plugin is automatically integrated to "gradle build". When you run it, it will run "gradle verifyGoogleJavaFormat". In case of violations, the build will fail.
We are using it on jenkins and it works. You will only need dependency to build.gradle file:
compile group: 'com.github.sherter.google-java-format', name: 'com.github.sherter.google-java-format.gradle.plugin', version: '0.8', ext: 'pom'
And also add plugin:
id 'com.github.sherter.google-java-format' version '0.8'
Then just run "gradle build" and you can see in the console, that verifyGoogleJavaFormat was executed.
DependsOn plugin's task that you need from a task that relates to a normal build, for example, you can use preBuild task:
tasks.findByName("preBuild").dependsOn(YOUR_TASK_FROM_PLUGIN)
or shorter
preBuild.dependsOn(YOUR_TASK_FROM_PLUGIN)
Also you can choose another task instead of preBuild.

Gradle uploadArchives task invoked for module even though explicitly not configured

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?

Getting spring boot jar files through gradle

The spring framework wants users to use dependency tools to download the framework, so I am trying to use gradle. I got this sample from their website http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto
buildscript {
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.BUILD-SNAPSHOT")
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
jar {
baseName = 'root'
version = '0.0.1-SNAPSHOT'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
I saved this to a file called build.gradle. Then in the CMD I went to the directory where the build.gralde file is located and type:
gradle build
It seemed to run fine but towards the building it's not working, here is the last logs I got from the command prompt:
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bootRepackage'.
Unable to find main class
* 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: 9.664 secs
I don't understand this "> Unable to find main class". I only want to get all the jar files and put them inside WEB-INF/lib of my eclipse projects. I am extremely new at gradle. What should I do?
It's not really clear to me, acoording to your comments, what are you trying to achieve, if you don't have a project sources, but for some reason wants to download dependent libraries. Gradle doesn't work this way, all the libraries are dowloded on demand, that means, they are dowloaded then you, for example, try to build your source files.
The exception you get, means, that gradle spring boot plugin's task bootRepackage Didn't find a main class in your project. This main class is mandatory for this task, since the task creates a standalone executable jar.
Sure, it is possble to dowload deps by custom task, like:
task getDeps(type: Copy) {
from sourceSets.main.runtimeClasspath
into 'runtime/'
}
But it seems, that you don't properly understand, how does it work. You should try to read gradle user guide first and let gradle to build your project for you, but not just combine some libs.

Run my task before a plugin's task?

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")

Resources