Gradle WAR with compiled classes under WEB-INF/classes - java

I'm trying to port a legacy Java webapp project into gradle.
This is a snippet of my build.gradle
def customBuildPath = 'build/classes'
war {
from(customBuildPath) {
into 'WEB-INF/classes'
}
from('WebContent') {
include 'Web/**/*'
into ''
}
}
dependencies {
compile fileTree(dir: 'projectlibs/lib', include:'*.jar')
compile fileTree(dir: 'build/classes', include:'**')
}
To maintain the custom structure I want to put all my *.class files under WEB-INF/classes and it works, but I find also the same *.class files under WEB-INF/lib.
My goal it to keep jars and classes in separated war folder.
Any thoughts?
Edit: Added dependencies{} to the build.gradle snippet.

Problem get solved with commenting out builded classes from the dependencies:
def customBuildPath = 'build/classes'
war {
from(customBuildPath) {
into 'WEB-INF/classes'
}
from('WebContent') {
include 'Web/**/*'
into ''
}
}
dependencies {
compile fileTree(dir: 'projectlibs/lib', include:'*.jar')
// compile fileTree(dir: 'build/classes', include:'**')
}

Related

Cannot use classes from .jar file

I've made a .jar with .java classes and tried to import them into my project, but it doesn't work.
When I build the project, I get:
> Task :compileJava FAILED
error: package skija does not exist
import static skija.scenes.*;
In my gradle I've tried to add this .jar via multiple methods and none of them work
build.gradle:
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
implementation fileTree(dir: 'lib', include: '*.jar')
implementation fileTree(dir: 'lib', includes: ['*.jar'])
implementation files('lib/scenes.jar')
implementation name: 'scenes'
}
Additionally, I've modified settings.gradle, which doesn't change anything:
pluginManagement {
buildscript {
repositories {
flatDir {
dirs 'lib'
}
}
dependencies {
classpath fileTree(dir: 'lib', includes: ['*.jar'])
classpath name: 'scenes'
}
}
}
Next, I've added libraries from the Idea Project Structure Libraries.
One is pointing to lib folder, and another points directly to the .jar file. This didn't help either.
I use a jar.bat script placed inside of PATH that calls jar.exe
jar.bat:
"C:\Program Files\Java\jdk-16.0.1\bin\jar.exe" %*
The scenes.jar was made with jar cfv scenes.jar * command.
Snipped of jar tf scenes.jar:
>"C:\Program Files\Java\jdk-16.0.1\bin\jar.exe" tf scenes.jar
META-INF/
META-INF/MANIFEST.MF
skija/scenes/
skija/scenes/BitmapImageScene.java
skija/scenes/BitmapScene.java
skija/scenes/BlendsScene.java
skija/scenes/BreakIteratorScene.java
skija/scenes/CodecScene.java
skija/scenes/ColorFiltersScene.java
skija/scenes/DebugTextBlobHandler.java
skija/scenes/DebugTextRun.java
skija/scenes/DecorationsBenchScene.java
skija/scenes/DrawableScene.java
skija/scenes/EmptyScene.java
skija/scenes/FigmaScene.java
skija/scenes/FontRenderingScene.java
skija/scenes/FontScene.java
skija/scenes/FontSizeScene.java
skija/scenes/FontVariationsScene.java
skija/scenes/GeometryScene.java
skija/scenes/HUD.java
skija/scenes/ImageBenchScene.java
skija/scenes/ImageCodecsScene.java
skija/scenes/ImageFiltersScene.java
skija/scenes/ImagesScene.java
skija/scenes/MaskFiltersScene.java
There is no point sticking .java files in a jar file. You need to compile the java files, which produces .class files. Put those in the jar file.

Gradle - How to force dependencies into the WEB-INF/lib

I'm looking to copy dependency jars into the war file's WEB-INF/lib directory whether they're transitive of the project or not. Is there a way to accomplish this?
My build.gradle is below and I've tried multiple options. Is there a way to ensure that a specific set of jars appears in the war no matter what?
build.gradle
plugins {
id 'war'
}
war {
manifest { attributes("Class-Path": "resources/") }
version = null
}
//Does not contain the jackson libraries
providedCompile project(":components:Manager")
dependencies {
//Doesn't work
implementation libraries.jackson_core
implementation libraries.jackson_databind
implementation libraries.jackson_annotations
providedCompile(
//Doesn't work
libraries.jackson_core,
libraries.jackson_databind,
libraries.jackson_annotations,
///
libraries.mybatis,
libraries.mybatis_cdi,
libraries.javax_ejb,
libraries.javax_rs
)
compile "javax:javaee-api:$jeeVersion" //The only jar that appears in the WEB-INF/lib folder
//Doesn't work
compile(
libraries.jackson_core,
libraries.jackson_databind,
libraries.jackson_annotations,
)
}
Solved my problem by setting up a custom warLib configuration
Credit
plugins {
id 'war'
}
configurations {
warLib
}
providedCompile project(":components:Manager")
dependencies {
providedCompile(
libraries.mybatis,
libraries.mybatis_cdi,
libraries.javax_ejb,
libraries.javax_rs
)
compile "javax:javaee-api:$jeeVersion"
warLib libraries.jackson_databind
}
war {
classpath configurations.warLib
manifest { attributes("Class-Path": "resources/") }
version = null
}

what's the gradle alternative to a fat JAR?

If transitive libs aren't packaged with the JAR task:
By default, jar task in gradle builds an executable jar file from your project source files. It will not contain any transitive libs that are needed for your program.
To the contrary, Netbeans does package JAR dependencies or transitive libs. Rather than a fat JAR how does gradle include libs?
plugins {
id 'com.gradle.build-scan' version '1.8'
id 'java'
id 'application'
}
mainClassName = 'net.bounceme.dur.mbaas.json.Main'
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
}
repositories {
jcenter()
}
jar {
manifest {
attributes 'Main-Class': 'net.bounceme.dur.mbaas.json.Main'
}
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
runtime group: 'com.google.firebase', name: 'firebase-admin', version: '5.2.0'
compile fileTree(dir: 'lib', include: '*.jar')
}
in relation to another question: what's the "way" to package libs with the JAR task which doesn't result in a "fat JAR"?
When using the application plugin, all libraries used by the application (own jar or transitive libraries) are put in a libs folder when packaged as an app. then these libs are referenced in the shell or batch script as classpath when launching the app.
The easiest way to create a fatjar in gradle, is to use the shadow plugin available via the plugin portal. see https://plugins.gradle.org/plugin/com.github.johnrengelman.plugin-shadow for details

Adding dependencies for gradle

I have in gradle.build:
dependencies {
classpath 'something:1.0'
}
What I need to add in the file to add a local .jar file, which java can "import"?
But I don't need to include this .jar in my application. Just like a shared library.
I tried:
classpath 'something:1.0','somethingelse:1.0'
classpath 'something:1.0, somethingelse:1.0'
classpath { 'something:1.0','somethingelse:1.0' }
compile files('somethingelse.jar')
classpath files('lib/somethingelse.jar')
classpath fileTree(dir: 'lib', include: '*.jar')
I think you want compile name: This recipie is in a gradle file I inherited.
repositories {
flatDir {
dirs 'lib'
}
dependencies {
compile name: 'jdom'
compile name: 'jebl-0.3'
}
BTW, this question appears to be a duplicate of
How to add local .jar file dependency to build.gradle file?

Gradle rename package before compile

I have eclipse java project.
Gradle script:
apply plugin: 'java'
jar {
manifest {
attributes 'Title': 'xxx'
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'lib', include: '**/*.jar')
}
def myPackage = 'mypackage'
sourceSets {
main {
java.srcDirs = ['src']
}
}
Project file structure
src
com.company.core
....classes
com.company.impl
....classes
In jar file need file structure
com.company.core
...classes
com.mypackage.impl
...classes
Need to rename package
company -> mypackge in jar file
It is not android.
Help please.
UPD
Thanks, Lance Java.
It working for me.
But there was a problem. All classes from libs (google-play-services.jar, android.jar) come into jar... I need classes located only in src folder... I can to exclude classes by all packages, but i think it is no good solution...
Is there another way whith shadowJar?
My dependencies:
dependencies {
compile files('libs/android.jar')
compile files('libs/google-play-services.jar')
}
This can be done with the shadow plugin
See relocating packages

Categories

Resources