Generate war file without libs directory in gradle - java

I have a gradle project and configured war plugin that is successfully generating the war file.
I want my dependencies jar files not to be the part of .war file in directory [/WEB-INF/lib/].
I will manually add all the dependencies in jboss's lib folder.
apply plugin: 'war'
sourceSets {
main {
java {
srcDir 'src'
}
}
}
war.baseName = 'web'
webAppDirName = 'WebContent'
war {
webXml = file('WebContent/WEB-INF/web.xml')
}
dependencies {
compile fileTree('../ThirdPartyJars')
compile project(':core')
}
This is generating a .war file that already contains all the dependency.
Is their any way to remove all these dependencies from war file in gradle.

This is an awful idea, IMO, but you can add the dependencies to the providedCompile or providedRuntime configuration rather than compile or runtime.

Related

Why there is no .class file in my jar file created by gradle?

I create a jar file by Gradle(Ver 6.1.1) but there is no .class file in my jar file.
Created jar file contains only META-INF directory.
My build.gradle under root directory is following.
apply plugin: 'java'
group = 'com.test.foo'
version = '1.0.0'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
compileJava {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
}
dependencies {
implementation('org.slf4j:slf4j-api:1.7.21')
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
}
Then, I entered a following command.
$ gradle build
Of course there are many .java files under /src, but created jar file does not contains .class files.
I confirmed this question, but this is not useful for me because my using command is supplied by the plugin.
Why this phenomenon is happen? What should I fix?
â– additional info
/build/classes directory does not also created.
You wrote
Of course there are many .java files under /src, but created jar file does not contains .class files.
Gradle, by convention, compiles Java source files from folder src/main/java. Make sure to either put your files in that folder (preferred) or configure Gradle to look into src only.
Further reading: Organizing Gradle Projects

gradle exclude external resource folder from all jars

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')
}
}
}

How to deploy a Jar from a library project

I have a library project in Java which is several folders, each one doing specific parts and having its own dependencies.
Since I am working locally I would like to deploy this library locally and get the Jar to import to another project.
For this reason I am using gradle and what I did was going to the directory where I have all the folders of the library and gradle init and then gradle build.
Since I want the files locally, I saw that I can use gradle publishToMavenLocal, which I did and it created a jar file under ~/.m2/..... Now the issue is that this jar file appear to only contain a META-INF folder and inside of it a manifest.mf file.
This is the build.gradle file used.
What am I doing wrong? Should I do something different?
check gradle docs
there is also a complete example.
be sure to add your sourceSets that you want to compile and build in the jar.
build.gradle
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets {
main {
java { srcDir 'src/main/java' }
resources {
srcDirs 'src/main/resources'
}
}
test {
java { srcDir 'src/test/java' }
resources {
srcDirs 'src/test/resources'
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'project1-sample'
version = '1.1'
from components.java
}
}
}
You could also add your library project to your main project like this :
build.gradle
dependencies {
compile project(':library_project')
}
settings.gradle
rootProject.name = 'Project'
include ":library_project"
project(':library_project').projectDir = new File(settingsDir, '../library_project')

How to build jar file with gradle with dependencies

I'm building a jar file with Gradle. This jar file is being used as a library in another project. But when the project tries to use the jar file, a ClassNotFoundException is returned.
Caused by: java.lang.NoClassDefFoundError: com/auth0/jwt/JWT
I've included the jwt library in the gradle file building the jar:
compile group: 'com.auth0', name: 'java-jwt', version: '3.4.0'
The project using the jar can't seem to find this jar dependency in the jar.
I built the jar with the gradle command:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Jar File creation',
'Implementation-Version': version,
'Main-Class': 'com.group.me.name.MyJarClass'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
How do I include the missing dependency in the jar?
You, generally, should not pack the dependencies inside a published JAR. It's better to declare them as dependencies in pom.xml and let the user of you JAR fetch them. It's just a good practice that should be enough.
If it's not enough, use Gradle Shadow:
plugins {
id 'com.github.johnrengelman.shadow' version '4.0.2'
}
Fat JAR is produced with shadowJar task in this case. Publishing is easy as well:
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
url "http://your.repo"
}
}
}

Gradle is not place WebJar contents into the correct place in my Jar file

I've got a basic Java application in which I would like to use WebJars. I use Gradle as my build system. I would like to use the WebJars for Bootstrap and JQuery so I can easily reference and update them in my Spring-Boot/ThymeLeaf application. The application is basically the one from the form tutorial located here
As I understand it Gradle should place all the files from the WebJars into the META-INF folder in my Jar file. If I understand everything correctly the Spring-Boot resource handler will then load resource from META-INF/ when I reference something in my html page that starts with /webjars/
Unfortunately this doesn't work (yet). Since I see in Tomcat's log output that the resource handler is correctly installed. I decided to check if the files are actually in my Jar file.
When I extract my Jar file the META-INF folder only has a file called MANIFEST.MF with some information about Spring Boot. There is a BootStrap-3.3.7.Jar and a JQuery-3.2-1.Jar file in BOOT-INF/lib but I don't think that is where they are supposed to end up. (Or am I wrong and is there error somewhere in the resource handler?).
How do I tell gradle to do the right thing with these files when I run gradle build?
My gradle.build file looks like this:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: "jacoco"
jar {
baseName = 'gs-serving-web-content'
version = '0.0.1'
}
bootRun {
addResources = true
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.webjars:jquery:3.2.1")
compile("org.webjars:bootstrap:3.3.7")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Gradle is doing the right thing as the jars should be packaged in BOOT-INF/lib. The root of each jar in BOOT-INF/lib is then added to the classpath from where each its webjar related content in META-INF content should be found.
I'd recommend asking another question that focuses on what your application's doing at runtime. As far as I can tell, everything's working as it should at build time.

Categories

Resources