How to exclude specific jars from WEB-INF/lib - gradle

I have the following gradle projects:
jar-module-A
+-- JavaEE lib(Dependency)
war-module-A
+-- jar-module-A
I want to exclude JavaEE lib from WEB-INF/lib.
Using providedCompile:
Since jar-module-A is not a web module but jar, I cannot use providedCompile in build.gradle of jar-module-A.
configurations { runtime.exclude JavaEE-lib }
It excludes JavaEE from not only runtime but also testRuntime, It fails my unit tests in war-module-A by ClassNotFoundException.
How can mitigate this situation?

Brute force solution would be:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
repositories {
mavenCentral()
maven {
url "file:///tmp/repo"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///tmp/repo")
}
}
}
dependencies {
compile group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
war {
classpath = classpath.filter {
it.name != 'javaee-api-6.0.jar'
}
}
for module_b. It might be filename dependent (not sure of that). Maybe will have a look later on, but not sure - short with time.
UPDATE
More sophisticated:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
repositories {
mavenCentral()
maven {
url "file:///tmp/repo"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///tmp/repo")
}
}
}
dependencies {
compile(group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT') {
transitive = false// here You can exclude particular dependency, not necessarily all
}
providedCompile group: 'javax', name: 'javaee-api', version: '6.0'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
No I see it can be done with many ways. It depends on what are other requirements regard build.

If the transitive is not working, can try resolution startegy.
I was fed up with the transitive or even normal exclude.
https://docs.gradle.org/2.2.1/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
dependencies{
compile(group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT')
}
configurations.all {
resolutionStrategy {
force ruimo:module_a:1.0-SNAPSHOT
}
}

Related

How to resolve invalid packaging for parent POM, must be "pom" but is "jar"

To resolve spring-framework vulnerability posted by spring.io
I tried upgrading spring-boot version from 2.4.5 to 2.5.12 and with gradle-6.8 version, on running ./gradlew clean build task is failing with error
Invalid packaging for parent POM org.apache.logging.log4j:log4j-api:2.17.2, must be "pom" but is "jar" in org.apache.logging.log4j:log4j-api:2.17.2
The dependency org.springframework.boot:spring-boot-starter-webflux loads the internal dependency log4j-api:2.17.2
How to resolve invalid parent POM packaging for internal dependencies?
build.gradle
buildscript {
ext {
springBootVersion = '2.5.12'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
{
exclude group: 'org.slf4j', module: 'slf4j-ext'
}
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.service'
version = ''
sourceCompatibility = 11
def logbackVersion = '1.2.3'
repositories {
mavenCentral()
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'org.apache.logging.log4j') {
details.useVersion '2.17.1'
}
}
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-webflux')
developmentOnly('org.springframework.boot:spring-boot-devtools')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('io.projectreactor:reactor-test')
implementation("ch.qos.logback:logback-core:${logbackVersion}")
implementation("ch.qos.logback:logback-classic:${logbackVersion}")
implementation('org.apache.httpcomponents:httpclient:4.5.11')
implementation('org.apache.commons:commons-collections4:4.4')
implementation("org.springframework.cloud:spring-cloud-vault-config:2.1.3.RELEASE")
implementation("org.springframework.cloud:spring-cloud-vault-config-consul:2.1.3.RELEASE")
implementation group: 'org.springframework.cloud', name: 'spring-cloud-consul-dependencies', version: '1.0.0.RELEASE', ext: 'pom'
implementation('com.amazonaws:aws-java-sdk-sqs:1.11.634')
implementation('org.projectlombok:lombok:1.18.12')
implementation('org.yaml:snakeyaml:1.26')
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
annotationProcessor('org.projectlombok:lombok:1.18.12')
implementation group: 'org.bouncycastle', name: 'bc-fips', version: '1.0.2'
implementation group: 'org.bouncycastle', name: 'bctls-fips', version: '1.0.11'
}
Adding mavenBom spring-cloud-dependencies helped resolve this issue. Suspecting webflux pulled in a transitive dependency to an older release and adding spring-cloud-dependencies bom in dependencyManagement ensured all Spring dependencies are at the same version
Here's update build.gradle file that worked
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'application'
}
group = 'com.service'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '11'
application {
mainClass = 'com.service.scheduler.SchedulerApplication'
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.1")
set('logbackVersion', "1.2.11")
}
bootJar {
archiveFileName = 'scheduler.jar'
}
bootRun {
systemProperties = System.properties
}
dependencies {
/*---spring dependencies---*/
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.cloud:spring-cloud-starter-vault-config'
implementation 'org.springframework.cloud:spring-cloud-vault-config-consul'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
implementation 'com.amazonaws:aws-java-sdk-sqs:1.12.187'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.yaml:snakeyaml:1.30'
implementation 'com.google.code.gson:gson:2.9.0'
/*---fips dependencies---*/
implementation group: 'org.bouncycastle', name: 'bc-fips', version: '1.0.2'
implementation group: 'org.bouncycastle', name: 'bctls-fips', version: '1.0.11'
/*---lombok dependencies---*/
implementation 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
/*---logback dependencies---*/
implementation("ch.qos.logback:logback-core:${logbackVersion}")
implementation("ch.qos.logback:logback-classic:${logbackVersion}")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

Why subproject dependency jar includes all dependencies in it?

My project consists of multiple modules like core, my-backend, university, learning, etc.
Core is module which is required in all rest modules.
When I create build of my-backend module, I see core is being added with all dependencies in it which is making that jar too big. Seems some issue with my gradle file which is causing this problem.
Also somehow application.yml from core module is not being loaded from my-backend module. Any Idea why?
My gradle file is as follows:
buildscript {
ext {
springBootVersion = '2.2.1.RELEASE'
internalUrl = 'http://internal/repository'
}
repositories {
mavenLocal()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "http://repo.spring.io/release" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.15'
}
}
plugins {
id "io.freefair.lombok" version "3.8.4" apply false
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'
apply from: "$rootDir/dependencies.gradle"
mainClassName = "com.myk.MyApp"
group = 'com'
version = app.version
description = """"""
sourceCompatibility = 11
targetCompatibility = 11
ext {
set('springCloudVersion', "Hoxton.RC1")
}
repositories {
mavenLocal()
maven { url "http://repo.spring.io/libs-snapshot" }
jcenter()
mavenCentral()
maven { url "http://repo.maven.apache.org/maven2" }
maven { url "http://maven.aksw.org/repository/internal" }
maven { url "https://dl.bintray.com/michaelklishin/maven/" }
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
// additional plugins
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "io.freefair.lombok"
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-integration'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.cloud:spring-cloud-stream-test-support'
testImplementation (group: 'com.github.javafaker', name: 'javafaker', version: ver.'javafaker') {
exclude group: 'org.yaml', module: 'snakeyaml'
}
}
test {
useJUnitPlatform()
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}
project(':core') {
dependencies {
implementation group: 'com.github.fommil.netlib', name: 'all', version: '1.1.2' // weka dependency
implementation 'org.springframework.boot:spring-boot-configuration-processor:2.2.1.RELEASE'
implementation group: 'com.google.firebase', name: 'firebase-admin', version: ver.'firebase-admin'
implementation group: 'com.google.apis', name: 'google-api-services-gmail', version: ver.'google-api-services-gmail'
compile "org.quartz-scheduler:quartz:${ver.quartz}"
}
}
[
':university',
':my-backend',
':learning'
].each {
project(it) {
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'io.spring.dependency-management'
mainClassName = "com.myk.MyApp"
springBoot {
buildInfo()
}
dependencies {
compile project(':core')
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile group: 'io.github.swagger2markup', name: 'swagger2markup', version: ver.'swagger2markup'
}
}
}

Gradle Spring Boot: Add folder at root to jar

So I have a folder .ebextensions at the root of my spring boot project,and those files are not being included in my jar when I use "bootpackage" in my gradle plugin for intellij.I am deploying the jar On Amazon Web Services
How do I include these files in my jar?
My build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'haughton.daniel'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Edgware.SR1'
}
dependencies {
//compile 'org.springframework.cloud:spring-cloud-starter-aws'
compile('org.springframework.boot:spring-boot-starter-data-jpa')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.0.RELEASE'
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
// https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '2.1.2.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-aws-autoconfigure
//compile group: 'org.springframework.cloud', name: 'spring-cloud-aws-autoconfigure', version: '1.2.2.RELEASE'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile('org.springframework.boot:spring-boot-starter-web')
compile ('org.apache.tomcat:tomcat-dbcp:8.0.30')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Solution:
Added this to my build.gradle
processResources {
from ('.ebextensions/') {
into '.ebextensions'
}
}
I added an ebextensions folder into source of my project, this contains the .ebextensions folder with nginx config:
Folder structure
I extended bootJar task:
bootJar {
baseName = 'project'
version = '1.0.0'
from('ebextensions')
//...
When I run ./gradlew cle ass bootJar, then the unzipped project-1.0.0.jar contains:
.ebextensions
BOOT-INF
META-INF
org
I used Gradle 4.10.
The root cause was 413 Request Entity Too Large with ElasticBeanstalk.
Full content of proxy.conf:
client_max_body_size 20M;

Create plugin for IntelliJ IDEA. Plugin is incompatible with this installation

I create a plugin for IntelliJ IDEA and I want it to run on Android Studio and other products based on IDEA.
I use gradle-intellij-plugin
and have such settings:
build.gradle
buildscript {
ext.kotlin_version = '1.2.10'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id "org.jetbrains.intellij" version "0.2.17"
id "org.jetbrains.kotlin.jvm" version "1.2.10"
}
group pluginGroup
version pluginVersion
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'kotlin'
intellij {
version 'IC-2017.3'
plugins 'git4idea'
pluginName "plugin name"
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
//...
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
plugins.xml
<idea-plugin>
...
<idea-version since-build="143.379"/>
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.vcs</depends>
<depends>Git4Idea</depends>
...
</idea-plugin>
On IntelliJ IDEA is installed without errors.
When install on Android Studio 3.0.1 there is an error
Plugin 'plugin name' is incompatible with this installation
How to fix the error?
I met the same problem too and I solved it from plugin.xml , you can also find the solution easily in plugin.xml .
In plugin.xml , you can see this :
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="173.0"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
we can get two links :
http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html
http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
I solve the problem by the 1st link , I think the 2nd link is also worth to collect .
Go into the 1st link , and scroll down you will see:
So just change
idea-version since-build="173.0"
to
idea-version since-build="141"
Now you can retry to prepare your plugin .
Сhanged the settings and it works for me
build.gradle
buildscript {
ext.kotlin_version = '1.2.10'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id "org.jetbrains.intellij" version "0.2.17"
}
group pluginGroup
version pluginVersion
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
intellij {
version '2017.3'
plugins 'git4idea'
pluginName "plugin name"
updateSinceUntilBuild false
type 'IC'
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.code.gson:gson:2.8.2'
compile("com.taskadapter:redmine-java-api:3.1.0") {
exclude group: "org.slf4j"
}
// testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
plugin.xml
<idea-version since-build="141.177"/>
<depends>com.intellij.modules.lang</depends>
<depends>Git4Idea</depends>

Gradle - Exclude provided scope jars from War file

Gradle seems to claim that it doesnt include the providedCompile and providedRuntime scopes when building the war file.
But, when i do the build with below war config, a folder named "lib-provided" seems to be containing all the provided scoped jars. How do I limit this functionality to NOT include the provided scoped jars.
configure(rootProject) {
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repository.jboss.org/nexus/content/groups/public-jboss/" }
}
// Import Spring Boot's bom, spring-boot-dependencies
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-dependencies:1.2.5.RELEASE'
}
}
// Override the spring-data-releasetrain.version property
ext['spring-data-releasetrain.version'] = 'Fowler-SR1'
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-security")
............ Other Spring Boot based projects
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.4.6")
......... Below are the "Provided" Scoped packages
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedCompile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.23")
providedCompile("org.hibernate:hibernate-core:4.3.7.Final")
providedCompile("org.hibernate:hibernate-infinispan:4.3.7.Final")
providedCompile("org.hibernate:hibernate-entitymanager:4.3.7.Final")
providedCompile("org.hibernate:hibernate-validator:5.1.3.Final")
providedCompile("org.hibernate:hibernate-search-orm:5.0.1.Final")
providedCompile("org.hibernate.common:hibernate-commons-annotations:4.0.4.Final")
providedCompile("org.infinispan:infinispan-core:7.1.1.Final")
providedCompile("org.infinispan:infinispan-query:7.1.1.Final")
testCompile("com.microsoft.sqlserver:sqljdbc41:4.1")
}
configurations {
providedCompile
// replaced with jcl-over-slf4j
all*.exclude group: 'commons-logging', module: 'commons-logging'
// replaced with log4j-over-slf4j
all*.exclude group: 'log4j', module: 'log4j'
}
}
war {
baseName = 'abc'
version = '5.0.0-SNAPSHOT-' + + System.currentTimeMillis();
doFirst {
manifest {
attributes("Implementation-Title": project.name, "Implementation-Version": version, "Implementation-Timestamp": new Date())
}
}
webAppDirName = 'web'
includeEmptyDirs false
archiveName 'abc.war'
}
Thanks!
In the below configuration, Gradle 4.6
None of the below depndnecies will be in the War/WEB-INF lib.
If you have an Ear build.gradle can contain entry like below , which will ensure all depndence jar in the Ear/lib folder.
if the below entry is made, The generated Ear file will have .war file and the War/lib folder will not be having the specified dependent files.
apply plugin: 'war'
earlib project(path: ':MyWebProject', configuration: 'compile')
deploy project(path: ':MyWebProject', configuration: 'archives')
apply plugin: 'war'
dependencies {
providedCompile('org.apache.logging.log4j:log4j-web')
providedCompile('org.springframework.boot:spring-boot-starter-web') {
exclude module: "spring-boot-starter-tomcat"
}
}

Resources