Gradle ear is putting JAR instead of WAR - war

I'm trying to build an ear using Gradle.
I've my project tree like:
/project
|
|--> /web-application
| |
| |--> /src (stuff of web app)
| |
| |--> build.gradle
|
|--> build-gradle
|--> settings.gradle
I'm trying to generate the ear using the ear plugin, but when I do gradle assemble I have the war created under the build directory of the web-application, but inside the generated ear I have a jar of the web application.
The gradle configuration files are very simple, here they are:
project/build.gradle
apply plugin: 'ear'
repositories {
mavenCentral()
}
dependencies {
deploy project(':web-application')
earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}
project/web-application/build.gradle
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
compile group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}
What I did wrong?
I notice that also the bundled samples for the war plugin, have the same problem...
Thanks in advance

SOLVED!
It needs to configure the WAR module inside the EAR project as:
dependencies {
deploy project(path:':web-application', configuration:'archives')
earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}

You can avoid specifying a configuration for each deploy dependency using something like this:
allprojects {
plugins.withType(WarPlugin) {
// Set default configuration as WAR archive if WAR plugin is used
configurations {
'default' {
extendsFrom = [archives]
}
}
}
}
...
dependencies {
deploy project(':web-application')
}

Related

React Native Firebase Android always crash 0.61

Issue
I tried to add the react-native-firebase library https://github.com/invertase/react-native-firebase and everything works perfectly on iOS. When it comes to integration on android, nothing works.
The project Builds correctly, but the app crashes (only in Android) right after the build with this output:
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1099 file(s) to forward-jetify. Using 16 workers...
info Starting JS server...
info Launching emulator...
info Successfully launched emulator.
info Installing the app...
> Configure project :#react-native-firebase_analytics
:#react-native-firebase_analytics:firebase.bom using default value: 21.1.0
:#react-native-firebase_analytics package.json found at /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/#react-native-firebase/analytics/package.json
:#react-native-firebase_analytics:version set from package.json: 6.0.3 (6,0,3 - 6000003)
:#react-native-firebase_analytics:android.compileSdk using custom value: 28:#react-native-firebase_analytics:android.targetSdk using custom value: 28
:#react-native-firebase_analytics:android.minSdk using custom value: 16
:#react-native-firebase_analytics:reactNativeAndroidDir /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/react-native/android
> Configure project :#react-native-firebase_app
:#react-native-firebase_app:firebase.bom using default value: 21.1.0
:#react-native-firebase_app package.json found at /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/#react-native-firebase/app/package.json
:#react-native-firebase_app:version set from package.json: 6.0.3 (6,0,3 - 6000003)
:#react-native-firebase_app:android.compileSdk using custom value: 28
:#react-native-firebase_app:android.targetSdk using custom value: 28
:#react-native-firebase_app:android.minSdk using custom value: 16
:#react-native-firebase_app:reactNativeAndroidDir /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/react-native/android
> Configure project :app
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
> Task :app:processDebugGoogleServices
Parsing json file: /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/android/app/google-services.j
son
> Task :app:installDebug
12:49:58 V/ddms: execute: running am get-config
12:49:58 V/ddms: execute 'am get-config' on 'emulator-5554' : EOF hit. Read: -1
12:49:58 V/ddms: execute: returning
Installing APK 'app-debug.apk' on '3.7_WVGA_Nexus_One_API_28(AVD) - 9' for app:debug
12:49:58 D/app-debug.apk: Uploading app-debug.apk onto device 'emulator-5554'
12:49:58 D/Device: Uploading file onto device 'emulator-5554'
12:49:58 D/ddms: Reading file permision of /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/android/app/build/outputs/apk/debug/app-debug.apk as: rw-r--r--
12:49:59 V/ddms: execute: running pm install -r -t "/data/local/tmp/app-debug.apk"
12:50:02 V/ddms: execute 'pm install -r -t "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : E
OF hit. Read: -1
12:50:02 V/ddms: execute: returning
12:50:02 V/ddms: execute: running rm "/data/local/tmp/app-debug.apk"
12:50:02 V/ddms: execute 'rm "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read:
-1
12:50:02 V/ddms: execute: returning
Installed on 1 device.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 7s
192 actionable tasks: 3 executed, 189 up-to-date
info Connecting to the development server...
info Starting the app on "emulator-5554"...
Starting: Intent { cmp=com.halfy_app/.MainActivity }
MBP-di-Enzo:halfy_app enzomanuelmangano$
MBP-di-Enzo:halfy_app enzomanuelmangano$ react-native run-android --log
error: unknown option `--log'
MBP-di-Enzo:halfy_app enzomanuelmangano$ react-native run-android -log
error: unknown option `-l'
MBP-di-Enzo:halfy_app enzomanuelmangano$ react-native run-android
error React Native CLI uses autolinking for native dependencies, but the following modules are linked manually:
- react-native-maps (to unlink run: "react-native unlink react-native-maps")
This is likely happening when upgrading React Native from below 0.60 to 0.60 or above. Going forward, you can unlink this dependency via "react-native unlink <dependency>" and it will be included in your app automatically. If a library isn't compatible with autolinking, disregard this message and notify the library maintainers.
Read more about autolinking: https://github.com/react-native-community/cli/blob/master/docs/autolinking.md
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1099 file(s) to forward-jetify. Using 16 workers...
info JS server already running.
info Installing the app...
> Configure project :#react-native-firebase_analytics
:#react-native-firebase_analytics:firebase.bom using default value: 21.1.0
:#react-native-firebase_analytics package.json found at /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/#react-native-firebase/analytics/package.json
:#react-native-firebase_analytics:version set from package.json: 6.0.3 (6,0,3 - 6000003)
:#react-native-firebase_analytics:android.compileSdk using custom value: 28
:#react-native-firebase_analytics:android.targetSdk using custom value: 28
:#react-native-firebase_analytics:android.minSdk using custom value: 16
:#react-native-firebase_analytics:reactNativeAndroidDir /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/react-native/android
> Configure project :#react-native-firebase_app
:#react-native-firebase_app:firebase.bom using default value: 21.1.0
:#react-native-firebase_app package.json found at /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/#react-native-firebase/app/package.json
:#react-native-firebase_app:version set from package.json: 6.0.3 (6,0,3 - 6000003)
:#react-native-firebase_app:android.compileSdk using custom value: 28
:#react-native-firebase_app:android.targetSdk using custom value: 28
:#react-native-firebase_app:android.minSdk using custom value: 16
:#react-native-firebase_app:reactNativeAndroidDir /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/node_modules/react-native/android
> Configure project :app
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
> Task :app:processDebugGoogleServices
Parsing json file: /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/android/app/google-services.j
son
> Task :app:installDebug
12:51:39 V/ddms: execute: running am get-config
12:51:39 V/ddms: execute 'am get-config' on 'emulator-5554' : EOF hit. Read: -1
12:51:39 V/ddms: execute: returning
Installing APK 'app-debug.apk' on '3.7_WVGA_Nexus_One_API_28(AVD) - 9' for app:debug
12:51:39 D/app-debug.apk: Uploading app-debug.apk onto device 'emulator-5554'
12:51:39 D/Device: Uploading file onto device 'emulator-5554'
12:51:39 D/ddms: Reading file permision of /Users/enzomanuelmangano/Desktop/Lavoro/halfy_app/android/app/build/outputs/apk/debug/app-debug.apk as: rw-r--r--
12:51:39 V/ddms: execute: running pm install -r -t "/data/local/tmp/app-debug.apk"
12:51:40 V/ddms: execute 'pm install -r -t "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read:
12:51:40 V/ddms: execute: returning
12:51:40 V/ddms: execute: running rm "/data/local/tmp/app-debug.apk"
12:51:40 V/ddms: execute 'rm "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read: -1
12:51:40 V/ddms: execute: returning
Installed on 1 device.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 4s
192 actionable tasks: 3 executed, 189 up-to-date
info Connecting to the development server...
info Starting the app on "emulator-5554"...
Starting: Intent { cmp=com.halfy_app/.MainActivity }
Android
android/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 22
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "28.0.0"
playServicesVersion = "17.0.0"
androidMapsUtilsVersion = "0.5+"
}
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenLocal()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}
maven { url 'https://jitpack.io' }
}
}
android/app/build.gradle
apply plugin: "com.android.application"
apply plugin: 'com.google.gms.google-services'
import com.android.build.OutputFile
project.ext.react = [
entryFile: "index.js",
enableHermes: false, // clean and rebuild if changing
]
apply from: "../../node_modules/react-native/react.gradle"
/**
* Set this to true to create two separate APKs instead of one:
* - An APK that only works on ARM devices
* - An APK that only works on x86 devices
* The advantage is the size of the APK is reduced by about 4MB.
* Upload all the APKs to the Play Store and people will download
* the correct one based on the CPU architecture of their device.
*/
def enableSeparateBuildPerCPUArchitecture = false
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false
/**
* The preferred build flavor of JavaScriptCore.
*
* For example, to use the international variant, you can use:
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
*
* The international variant includes ICU i18n library and necessary data
* allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
* give correct results when using with locales other than en-US. Note that
* this variant is about 6MiB larger per architecture than default.
*/
def jscFlavor = 'org.webkit:android-jsc:+'
/**
* Whether to enable the Hermes VM.
*
* This should be set on project.ext.react and mirrored here. If it is not set
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
* and the benefits of using Hermes will therefore be sharply reduced.
*/
def enableHermes = project.ext.react.get("enableHermes", false);
android {
compileSdkVersion rootProject.ext.compileSdkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.halfy_app"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://facebook.github.io/react-native/docs/signed-apk-android.
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// https://developer.android.com/studio/build/configure-apk-splits.html
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+" // From node_modules
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation(project(':react-native-maps')){
exclude group: 'com.google.android.gms', module: 'play-services-base'
exclude group: 'com.google.android.gms', module: 'play-services-maps'
}
implementation 'com.google.android.gms:play-services-base:10.0.1'
implementation 'com.google.android.gms:play-services-maps:10.0.1'
if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
implementation jscFlavor
}
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
project.ext.vectoricons = [
iconFontNames: [ 'MaterialIcons.ttf', 'FontAwesome.ttf' ] // Name of the font files you want to copy
]
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
apply from: file("../../node_modules/#react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
React Native Version: 0.61
I solved that problem erasing that code in android/app/build.gradle
implementation(project(':react-native-maps')){
exclude group: 'com.google.android.gms', module: 'play-services-base'
exclude group: 'com.google.android.gms', module: 'play-services-maps'
}
implementation 'com.google.android.gms:play-services-base:10.0.1'
implementation 'com.google.android.gms:play-services-maps:10.0.1'

OracleJET: How to change grunt release directory

I created an javascript app using Oracle JET v2.0. Running
grunt build:release
creates release folder in the project root folder. How to change this to point to another path, for example outside project root?
If you're using the Yeoman generator, you can follow the Grunt flow in scripts/grunt/tasks/build.js. You'll see under if (target === "release")
that it runs a number of tasks in order.
Unfortunately the release folder name is hardcoded in many of those tasks' config files. So it might make more sense to add a task at the end of your build:release task to copy the built release into a new directory. You could add a target to the scripts/grunt/config/copy.js file named myFinalRelease:
module.exports = {
release:
{
src: [
"**",
"!bower_components/**",
"!grunt/**",
"!scripts/**",
"!js/**/*.js",
"js/libs/**",
"!js/libs/**/*debug*",
"!js/libs/**/*debug*/**",
"!node_modules/**",
"!release/**",
"!test/**",
"!.gitignore",
"!bower.json",
"!Gruntfile.js",
"!npm-shrinkwrap.json",
"!oraclejetconfig.json",
"!package.json"
],
dest: "release/",
expand: true
},
myFinalRelease:
{
cwd: 'release/',
src: ['**'],
dest: "myRelease",
expand: true
}
};
Then add that task:target as a copy step on the last line of the release section of scripts/grunt/tasks/build.js:
...
if (target === "release")
{
grunt.task.run(
[
"clean:release",
"injector:mainReleasePaths",
"uglify:release",
"copy:release",
"requirejs",
"clean:mainTemp",
"copy:myFinalRelease"
]);
}
...
To make this more robust, you should do some additional cleanup tasks if you're doing a lot of build:release'ing, because the myRelease folder won't get cleaned out unless you create tasks to do it. Or you might look into other Grunt plugins like grunt-contrib-rename.
If this is too much copying and too messy for your tastes, you could instead edit all of the hardcoded task configs to change the name of the release directory. You'll find the directory name shows up in these four files:
scripts/grunt/config/clean.js
...
release: ["release/*"],
...
scripts/grunt/config/uglify.js
...
dest:"release/js"
...
scripts/grunt/config/copy.js
...
dest: "release/",
...
scripts/grunt/config/require.js
...
baseUrl: "./release/js",
name: "main-temp",
mainConfigFile: "release/js/main-temp.js",
optimize: "none",
out: "release/js/main.js"...
...

proguard - dupe zip entry ( after upgraded facebook parse UI lib to v4 )

---SOLVED : deleted 2 lines from :app:build.gradle---
compile files('ParseLoginUI/libs/Parse-1.9.1.jar')
compile files('ParseLoginUI/libs/ParseFacebookUtilsV4-1.9.1.jar')
the lib comment here can be interpreted as "you need these 2 statements in your proj.root.build.gradle. Well not true. I had to delete those in order not to get 'dupe zip' errors from proguard.
--Solved--
Upgrade of parseUI library from v3 to v4 went fine til i tried to run proguard within gradle "assembleRelease" task.
Error:
:proguardRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':proguardRelease'.
> java.io.IOException: Can't write [/home/rob/src/CastVideos-android/build/intermediates/classes-proguard/release/classes.jar]
(Can't read [/home/rob/src/CastVideos-android/build/intermediates/exploded-aar/CastVideos-android/ParseLoginUI/unspecified/libs/Parse-1.9.1.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [Parse-1.9.1.jar:com/parse/AnonymousAuthenticationProvider.class]))
I checked proguard faqs for "dupe zip" ...
I tried to figure out how the dependency tree might be handling a dupe of ./libs/Parse-1.9.1.jar in that library.
IM stuck
build.gradle...
packagingOptions {
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/MANIFEST'
...
buildTypes {
release {
minifyEnabled true
proguardFile 'proguard-android-optimize.txt'
signingConfig signingConfigs.release
}
...
dependencies {
compile files('libs/android-query.0.25.10.jar')
compile files('libs/jackson-core-lgpl-1.9.2.jar')
compile files('libs/jackson-mapper-lgpl-1.9.2.jar')
compile 'com.github.amlcurran.showcaseview:library:5.0.0'
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
// to get around https://code.google.com/p/android/issues/detail?id=52962, we need to
// depend on both debug and release versions of the library
releaseCompile project(path: '..:CastCompanionLibrary-android', configuration: 'release')
debugCompile project(path: '..:CastCompanionLibrary-android', configuration: 'debug')
compile project(':ParseLoginUI')
compile files('ParseLoginUI/libs/Parse-1.9.1.jar')
compile files('ParseLoginUI/libs/ParseFacebookUtilsV4-1.9.1.jar')
compile 'com.facebook.android:facebook-android-sdk:4.0.1'
}
dependency graph for "release"...
...
+--- project :ParseLoginUI
| +--- com.parse.bolts:bolts-android:1.2.0
| \--- com.android.support:support-v4:22.0.0
| \--- com.android.support:support-annotations:22.0.0
+--- com.facebook.android:facebook-android-sdk:4.0.1
| +--- com.android.support:support-v4:[21,22) -> 22.0.0 (*)
| \--- com.parse.bolts:bolts-android:1.1.4 -> 1.2.0
\--- project :..:CastCompanionLibrary-android
+--- com.android.support:appcompat-v7:22.+ -> 22.0.0
| \--- com.android.support:support-v4:22.0.0 (*)
+--- com.android.support:mediarouter-v7:22.+ -> 22.0.0
| \--- com.android.support:appcompat-v7:22.0.0 (*)
\--- com.google.android.gms:play-services-cast:7.+ -> 7.0.0
\--- com.google.android.gms:play-services-base:7.0.0
\--- com.android.support:support-v4:22.0.0 (*)
Under project.root, the only jar files are :
./ParseLoginUI/libs/ParseFacebookUtilsV4-1.9.1.jar
./ParseLoginUI/libs/Parse-1.9.1.jar
proguard config...
-keep class com.facebook.** { *; }
-keep class com.parse.** { *; }
-dontwarn com.parse.**
-dontnote com.parse.**

Defining custom classpath for a jar manifest in gradle

I'm trying to define a jar task for all sub projects (about 30). I tried the following task:
jar {
destinationDir = file('../../../../_temp/core/modules')
archiveName = baseName + '.' + extension
metaInf {
from 'ejbModule/META-INF/' exclude 'MANIFEST.MF'
}
def manifestClasspath = configurations.runtime.collect { it.getName() }.join(',')
manifest {
attributes("Manifest-Version" : "1.0",
"Created-By" : vendor,
"Specification-Title" : appName,
"Specification-Version" : version,
"Specification-Vendor" : vendor,
"Implementation-Title" : appName,
"Implementation-Version" : version,
"Implementation-Vendor" : vendor,
"Main-Class" : "com.dcx.epep.Start",
"Class-Path" : manifestClasspath
)
}
}
My problem is, that the dependencies between the sub projects are not included in the manifest's classpath. I tried changing the runtime configuration to a compile configuration but that results in the following error.
What went wrong: A problem occurred evaluating project ':EskoordClient'.
You can't change a configuration which is not in unresolved state!
That is my complete build file for project EskoordClient:
dependencies {
compile project(':ePEPClient')
}
Most of my sub projects build files only define the projects dependencies. 3rd party lib dependencies are defined in the build file of the super project.
Is there a possibility to include all needed classpath entries (3rd party libraries and other projects) to a manifest classpath in a superproject for all subprojects.
This is how I got it to work. Get Project dependencies only using the call:
getAllDependencies().withType(ProjectDependency)
then adding the contents of each project's libsDir to my Class-Path manifest entry.
jar {
manifest {
attributes 'Main-Class': 'com.my.package.Main'
def manifestCp = configurations.runtime.files.collect {
File file = it
"lib/${file.name}"
}.join(' ')
configurations.runtime.getAllDependencies().withType(ProjectDependency).each {dep->
def depProj = dep.getDependencyProject()
def libFilePaths = project(depProj.path).libsDir.list().collect{ inFile-> "lib/${inFile}" }.join(' ')
logger.info"Adding libs from project ${depProj.name}: [- ${libFilePaths} -]"
manifestCp += ' '+libFilePaths
}
logger.lifecycle("")
logger.lifecycle("---Manifest-Class-Path: ${manifestCp}")
attributes 'Class-Path': manifestCp
}
}

gradle - how do I build a jar with a lib dir with other jars in it?

In gradle - how can I embed jars inside my build output jar in the lib
directory (specifially the lib/enttoolkit.jar and lib/mail.jar)?
If you have all the jars inside a directory (lets call it libs) in your project, you only need this:
jar {
into('lib') {
from 'libs'
}
}
I guess it is more likely that these jars are dependencies of some sort. Then you could do it like this:
configurations {
// configuration that holds jars to copy into lib
extraLibs
}
dependencies {
extraLibs 'org.something:something-dep1:version'
extraLibs 'org.something:something-dep2:version'
}
jar {
into('lib') {
from configurations.extraLibs
}
}
Lifted verbatim from: http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar
Gradle 0.9:
jar {
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
Gradle 0.8:
jar.doFirst {
for(file in configurations.compile) {
jar.merge(file)
}
}
The above snippets will only include the compile dependencies for that project, not any transitive runtime dependencies. If you also want to merge those, replace configurations.compile with configurations.runtime.
EDIT: only choosing jars you need
Make a new configuration, releaseJars maybe
configurations {
releaseJars
}
Add the jars you want to that configuration
dependencies {
releaseJars group: 'javax.mail', name: 'mail', version: '1.4'
//etc
}
then use that configuration in the jar task outlined above.
simple:
task copyToLib( type: Copy ) {
into "$buildDir/libs/lib"
from configurations.runtime
}
jar { dependsOn copyToLib }
run it:
$ gradle jar
...
$ tree build/libs
build/libs
├── your-project-0.0.1.BUILD-SNAPSHOT.jar
└── lib
├── akka-actor-2.0.jar
├── akka-camel-2.0.jar
├── ... ... ...
├── spring-expression-3.1.0.RELEASE.jar
└── zmq-2.1.9.jar
1 directory, 46 files
I also needed to do something similar and wasn't quite able to get what Guus and stigkj suggested working, but got close enough with their help to get this working (Guus' example blew up on the dependencies { compile { extendsFrom myLibs }} closure for me.
apply plugin: 'groovy'
repositories {
mavenCentral()
}
configurations {
// custom config of files we want to include in our fat jar that we send to hadoop
includeInJar
}
dependencies {
includeInJar 'org.codehaus.groovy:groovy:1.8.6'
configurations.compile.extendsFrom(configurations.includeInJar)
}
jar {
into('lib') {
println "includeInJar: " + configurations.includeInJar.collect { File file -> file }
from configurations.includeInJar
}
}
Then running gradle jar and examining the created jar gives me this output, showing that I get the jar file to have groovy as well as all jars that it's dependent on inside the "fat jar":
% gradle jar
includeInJar: [/Users/tnaleid/.gradle/caches/artifacts-8/filestore/org.codehaus.groovy/groovy/1.8.6/jar/553ca93e0407c94c89b058c482a404427ac7fc72/groovy-1.8.6.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/antlr/antlr/2.7.7/jar/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm/3.2/jar/9bc1511dec6adf302991ced13303e4140fdf9ab7/asm-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-tree/3.2/jar/cd792e29c79d170c5d0bdd05adf5807cf6875c90/asm-tree-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-commons/3.2/jar/e7a19b8c60589499e35f5d2068d09013030b8891/asm-commons-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-util/3.2/jar/37ebfdad34d5f1f45109981465f311bbfbe82dcf/asm-util-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-analysis/3.2/jar/c624956db93975b7197699dcd7de6145ca7cf2c8/asm-analysis-3.2.jar]
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
BUILD SUCCESSFUL
Total time: 3.387 secs
% jar tvf build/libs/gradletest.jar
0 Mon Mar 12 11:40:00 CDT 2012 META-INF/
25 Mon Mar 12 11:40:00 CDT 2012 META-INF/MANIFEST.MF
0 Mon Mar 12 11:40:00 CDT 2012 lib/
5546084 Mon Mar 05 13:13:32 CST 2012 lib/groovy-1.8.6.jar
445288 Mon Mar 05 13:13:38 CST 2012 lib/antlr-2.7.7.jar
43398 Mon Mar 05 13:13:40 CST 2012 lib/asm-3.2.jar
21878 Mon Mar 05 13:13:40 CST 2012 lib/asm-tree-3.2.jar
33094 Mon Mar 05 13:13:40 CST 2012 lib/asm-commons-3.2.jar
36551 Mon Mar 05 13:13:40 CST 2012 lib/asm-util-3.2.jar
17985 Mon Mar 05 13:13:40 CST 2012 lib/asm-analysis-3.2.jar
Below code could be tried. It depends on the jar task and is of Type Jar
task createJobJar(dependsOn:jar,type:Jar) {
manifest {
attributes(
"Implementation-Title": 'Job '
,"Implementation-Version": version
)
}
classifier 'job'
destinationDir new File("$buildDir")
into('libs'){
from configurations.compile
}
into('classes'){
from "$buildDir/classes"
}
into('resources'){
from "$projectDir/src/main/resources"
}
into('scripts'){
from "$projectDir/src/main/scripts"
}
}
The above code would pack different content inside different directories.
Tested on gradle 2.2
I needed to the same thing you asked, and used this method. you may not need a custom configuration declaration, but i needed to separate the locally used jar files from those declared in a super-build file.
configurations{
//declare custom config if necessary, otherwise just use compile
myLibs
}
dependencies {
//add lib/*.jar files to myLibs
myLibs fileTree(dir: 'lib', include: '*.jar')
compile {
//set compile configuration to extend from myLibs
extendsFrom myLibs
}
}
// task to copy libs to output/lib dir
task copyToLib(type: Copy) {
into "$buildDir/output/lib"
from configurations.myLibs
}
jar {
//include contents of output dir
from "$buildDir/output"
manifest {
//...
}
}
//set build task to depend on copyToLib
build.dependsOn(copyToLib)
I had the same problem. I solved it like this:
Copy the files into the lib folder with copyToLib and reference the dependency with the Class-Path
ext.mainClass = 'test.main'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.code.gson:gson:2.8.2'
//....
}
jar {
from "$buildDir/libs/lib"
manifest {
attributes 'Main-Class': 'test.main',
'Class-Path': configurations.compile.collect { 'lib/'+it.getName() }.join(' ')
}
}
task copyToLib(type: Copy) {
into "$buildDir/libs/lib"
from configurations.compile
}
build.dependsOn(copyToLib)
In my case I needed to include a contents of the root project Jar into subproject Jar. So, to make it work, one can use this template:
jar{
manifest{
attributes 'Main-Class':'<main class>'
}
def conf= configurations.find {it.name.equals('compile') }
File jar= conf.files.find {it.name.contains('<name or part of the name of produced Jar>')}
FileTree fileTree=zipTree(jar)
from fileTree
}
My example:
jar{
manifest{
attributes 'Main-Class':'alexiy.jace.Jace'
}
description='Make a runnable API Jar'
def conf= configurations.find {it.name.equals('compile') }
File tools= conf.files.find {it.name.contains('Tools')}
FileTree fileTree=zipTree(tools)
from fileTree
}
Here's how I managed to achieve this build setting using Gradle. This will build your app into a Jar, copy the library jars generated into the libs directory under build/libs and also configure classpath to include the jars in the created build/libs/libs directory.
plugins {
id 'application'
}
group 'org.pkg'
version '1.0-SNAPSHOT'
ext.mainClass = 'org.pkg.MainClass'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation group: 'com.mchange', name: 'c3p0', version: '0.9.5.5'
}
task copyToLib(type: Copy) {
into "$buildDir/libs/libs"
// configurations.runtimeClasspath includes maven jars
from configurations.runtimeClasspath
}
def archiveVersion = "1.0.0"
task uberJar(type: Jar) {
archiveClassifier = 'uber'
from sourceSets.main.output
// include the copy task
dependsOn(copyToLib)
// use onfigurations.runtimeClasspath to collect all the jars
manifest {
attributes(
'Class-Path': configurations.runtimeClasspath.collect { 'libs/' + it.getName() }.join(' '),
'Main-Class': 'org.pkg.MainClass',
"Implementation-Title": "Gradle",
"Implementation-Version": archiveVersion
)
}
}
task <taskname>(type: Jar) {
archiveName 'nameofjar.jar'
doFirst {
manifest {
attributes 'Class-Path': configurations.compile.files.collect{ project.uri(it) }.join(' ')
}
}
}

Resources