gradle war: how to build jar, not classes - gradle

Gradle war plugin: how to build a jar and add it to war?
projectRoot/
src/main/java
src/main/resources
src/main/webapp
build a jar (foo.jar) from the java source code and resources.
add the jar under the WEB-INF/lib of the war.
WEB-INF/lib/foo.jar
The war task will not build a jar by default, and add all java classes and resources under WEB-INF/classes.
UPDATE
The War plugin extends the Java plugin to add support for assembling web
application WAR files. It disables the default JAR archive generation of the
Java plugin and adds a default WAR archive task.
There is a way to enable the Jar generation and let task war depends on task jar?

not sure if eastwater still needs the answer, hope others with the same problem will find this helpful
you can add/configure the war task in build.gradle
war {
classpath = classpath - sourceSets.main.output
from (jar) {
into 'WEB-INF/lib'
}
}
once build succeed, in build/libs folder you'll see the generated jar and the war containing the generated jar instead of classes

Add those into your root build.gradle
plugins {
id 'java'
id 'idea'
}
jar{
String somestr=''
configurations.runtime.each{somestr=somestr+" lib\\"+it.name}
manifest{
attributes 'Main-Class':'your_class_name'
attributes 'Class-Path':somestr
}
}
task copyJar(type:Copy){
from configurations.runtime
into ('build/libs/lib')
}
task release(type: Copy,dependsOn:[build,copyJar]){
}
add finally run this command
gradle release

Related

Gradle: how to exclude jar from an ear file?

I have a question regarding excluding a jar file from an ear artifact. My project has the following modules:
project
- ejb_module
- war_module
- ear_module
- other_module
In my ear_module/build.gradle :
dependencies {
deploy project(ejb_module)
earlib project(ejb_module)
deploy project(war_module)
earlib project(war_module)
}
The problem is how I exclude the jar artifact produced from the war_module.
The war_module produces both war and jar artifact. The reason I need the jar artifact is that my other_module depends on it.
A complicated factor is that the war_module has some dependencies that needs to go via earlib.
So my question is how I exclude the war_module.jar from the ear file? It is included both at root and in APP-INF/lib.
I presume in your war_module you are applying both war and java plugins to get a .war. and .jar artifacts ?
Try:
deploy project(path: 'war_module', configuration: 'archives')
earlib project(path: 'war_module', configuration: 'archives')
It should pick up a .war only because if look into the Gradle war plugin code you can see that it overrides the default output of the project, if java plugin was included.
Additionally, war plugin uses java plugin to compile your code under the hood.
You, of course, can simply exclude the artefact from the build by using
config (project){exclude module: 'name'}
but I think this is not the best you can do here.
Try looking into providedRuntime and runtimeOnly configurations depending on which version of Gradle do you use.
If the artefact is used for compilation then compileOnly etc.
This configuration used in dependecies{} will use the jar but will not be going to expose it anywhere else then it should not land in any artefact that you build and this is probably what would fit you the most in your case, but the question wasn't asking about this it's just an advice.
Assume your EAR contains an another existing jar, said xxx.yyy.zzz.jar.
In your ear project's build.gradle, add the following
ext.replacement = 'your.group:xxx.yyy.zzz:your.version'
configurations.all {
resolutionStrategy {
eachDependency { DependencyResolveDetails details ->
// remove to.be.excluded.jar
if (details.requested.group == 'your.group' &&
details.requested.name == 'to.be.excluded' &&
details.requested.version == 'your.version') {
details.useTarget replacement
}
}
}
}
This way, the to.be.excluded.jar will be replaced by xxx.yyy.zzz.jar, that virtually removes it from the EAR.

gradle jar cant find java source

I am going through the gradle jar build example at https://guides.gradle.org/building-jvm-libraries/
The java source is not in a default src/main/java directory, it's in org\example\mylib directory. How can I customise gradle to run gradle jar from this directory and compile the java source files to a jar?
The whole directory structure is \mylib\src\main\java\org\example\mylib
When I am in that directory, and run gradle jar there is a success message but then when I check with jar -tf build/libs/mylib-0.1.0.jar all I see is the manifest files. There are no java classes.
If I try and run gradle jar in the \mylib directory alone, then it fails with error message Task 'jar' not found in root project
The build.gradle file is:
apply plugin: 'java'
version = '0.1.0'
Try adding this to the gradle.build:
sourceSets {
main {
java {
srcDirs = ['src\main\java\org\example\mylib']
}
}
}

gradle to bundle nested jar dependencies into module output jar

How to make gradle to included some dependencies into module resulted jar as jar? i.e. to make jar with nested jar's e.g. in lib folder ?
This is not Android project, and this should be done for many modules in multi-module project, so fatJar, uberJar, shadowJar alike solutions seem not to fit.
You just need to add an additional from directive to include dependencies in your jar:
task jarJar(type: Jar) {
baseName = project.name + '-jarjar'
from { configurations.compile }
with jar
}

Why does my jar file not not contain any class files?

I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:
version = "0.0.1"
apply plugin: 'java'
task('gen', type: Jar) {
}
task gen2(type: Jar)
Running
gradle jar
generates a JAR-file that contains .class-files, while running
gradle gen
or
gradle gen2
generate a JAR-file that does NOT contain any .class-files.
Whats wrong with my class definition?
To build a jar with all the classes from main, as a default jar task would, do this:
task gen2(type: Jar){
baseName = 'gen2Jar'
from sourceSets.main.output
}
You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.
Alternatively, to copy settings from the default jar task:
task gen(type: Jar){
baseName = 'genJar'
with jar
}
Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main

Getting Gradle to publish a source Jar instead of a standard compiled Jar

I am slowly moving two projects over to Gradle. Project A produces a jar file that is used by Project B.
Project A is a GWT component and Project B is a GWT application.
How do I get Gradle to generate a Jar that contains the Java source code of Project A rather than the compiled classes?
I have tried adding the following to the build.gradle file, but this generates a second Jar file containing the source - I want the main artefact to contain the source, so that it is published to my local Maven repository when I run the install task.
task sourceJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourceJar
}
Is there a way to override the standard Jar creation task?
You can define your own 'myArtifacts' configuration and publish that instead. Note that since the install task is of type Upload, you should be able to change the default artifacts configuration from archives to sourceArchives:
configurations{
sourceArchives
}
artifacts{
sourceArchives sourceJar
}
install.configuration = configurations.sourceArchives
Hopefully, install should now just publish members of sourceArchives configuration.

Resources