Gradle - Exclude spring.xml from a dependency - java

Is there a way to exclude spring.xml from a gradle dependency? I have enabled autoscanning for all spring.xmls but do not want spring.xml from a specific jar to considered.
compile('com.savnet.ofm:ofm-client:16.08') {
exclude group: "net.sf.saxon", module: "saxon-xpath"
exclude group: 'hibernate'
}

You mean excluding a file from a JAR you depend on?
If so, then as far as I know there is no per-se supported way.
But you can make it manually.
Something like the following which is totally made up right now and not tested, so take this as a starting point that might work or might need some polishing to work.
configurations { foo }
dependencies { foo 'com.savnet.ofm:ofm-client:16.08'}
sourceSets { main.compileClasspath += configurations.foo }
task fooJar(type: Jar) {
archiveName = 'foo.jar'
from zipTree(configurations.foo.singleFile)
exclude 'spring.xml'
}
And then use fooJar output as runtime library.

Related

What is the exact purpose of Gradle configurations "all"?

I'm trying to get rid of all the errors and warnings suggested by the IDE (Intellij).
Among them, a warning occurs in the log4j2 dependency removal code, as shown below.
'exclude' cannot be applied to '(['group':java.lang.String, 'module':java.lang.String])'
I searched for a way to get rid of the error, and I succeeded in removing it.
But I don't know why it disappeared.
(the above alert. However, there is no problem in actual operation)
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
configurations {
implementation {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
configurations {
all {
implementation {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
}
The first method is to display an error message.
The second and third methods do not have any error messages and work normally.
Could you point me to the difference between these three methods?
As far as the IntelliJ warning goes - it seems like this is a caching issue that can resolve itself.
Configurations
configurations is a ConfigurationContainer - it contains many instances of configurations, with the type Configuration. Each configuration has a name.
(Aside: yes, the name 'configuration' is confusing. It doesn't mean "properties or settings used to configure the Gradle project", it means "a collection of files that might be outgoing artifacts, or incoming dependencies")
configurations.all {} will retrieve all configurations, and attempt to configure each one using the contents of the lambda.
Unless we're inside a task, resolving all configurations is usually (but not always!) bad practice, because it might trigger work that could have been avoided, which will make the build slower. Instead, try and use configurations.configureEach {}.
configurations.implementation {} will retrieve a single configuration that is named implementation. The implementation configuration is created by the Java plugin. Again, the lambda will configure the configuration.
In both cases - the contents of the lambda will have a receiver type of Configuration.
configurations {
all {
// configure *all* configurations
// this is a Configuration
Configuration currentReceiver = this
}
implementation {
// configure the 'implementation' configurations
// this is also a Configuration
Configuration currentReceiver = this
}
}
Nested configurations
Your third example is unusual.
configurations {
all {
implementation {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
}
What this does is that any time a Configuration is added to the project's ConfigurationContainer, the lambda is triggered. The lambda that you've defined will configure the implementation configuration, and add an exclusion.
This is equivalent to this:
configurations {
ConfigurationContainer confContainer = this
all {
confContainer.implementation {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
}
Which is weird... try to avoid nesting configurations like this! In this simple example, it probably works out okay, but it will cause problems if the inner lambda is dependent on the other lambda, because implementation is part of all - so it might cause some weird recursions.

Can I hold iText5 and iText7 in the same maven project?

We have a maven project that uses iText5. We would like to migrate to iText7. There are many parts that use the old library. Are there any contraindications in temporarily keeping both libraries as a reference in the pom and using the old version in some parts and the new in others? We will gradually replace the old one. We will permanently delete the old one after a long process of refactoring and testing.
when using itext5 and itext7 gives the same file errors.
need to exclude that file using the packaging option.
The below code will fix the compile-time build failed
android {
packagingOptions {
exclude 'com/itextpdf/io/font/cmap/HYGoThic-Medium.properties'
}
}
The below code will fix the Run-time build and make the application install
android {
packagingOptions {
exclude 'com/itextpdf/io/font/cmap/cjk_registry.properties'
exclude 'com/itextpdf/io/font/cmap/HeiseiKakuGo-W5.properties'
exclude 'com/itextpdf/io/font/cmap/HeiseiMin-W3.properties'
exclude 'com/itextpdf/io/font/cmap/HYGoThic-Medium.properties'
exclude 'com/itextpdf/io/font/cmap/HYSMyeongJo-Medium.properties'
exclude 'com/itextpdf/io/font/cmap/HYSMyeongJoStd-Medium.properties'
exclude 'com/itextpdf/io/font/cmap/KozMinPro-Regular.properties'
exclude 'com/itextpdf/io/font/cmap/MHei-Medium.properties'
exclude 'com/itextpdf/io/font/cmap/MSung-Light.properties'
exclude 'com/itextpdf/io/font/cmap/MSungStd-Light.properties'
exclude 'com/itextpdf/io/font/cmap/STSong-Light.properties'
exclude 'com/itextpdf/io/font/cmap/STSongStd-Light.properties'
exclude 'com/itextpdf/io/font/cmap_info.txt'
}
}

Multiple dependency versions with Gradle 5

I need to support 2 different versions of the same library (to support a legacy version), es4hadoop for Apache Spark.
Version 6.2.2(https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-spark-13_2.10/6.2.2)
Version 6.3.2 (https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-spark-13_2.10/6.3.2)
Both versions have same dependencies (scala-lang and Spark).
Not sure at all about the naming of that, but I would like something like:
implementation(group: 'org.elasticsearch', name: 'elasticsearch-spark-13_' + scalaVersion, version:'6.2.2') {
exclude group: "org.scala-lang"
}
implementation(group: 'org.elasticsearch', name: 'elasticsearch-spark-13_' + scalaVersion, version:'6.3.2') {
exclude group: "org.scala-lang"
relocate org.elasticsearch org.elasticsearch6 // ???
}
so I can use both new and old elasticsearch library, in the same project / JVM.
I know already it's possible to relocate lib with the shadowJar plugin, but is it possible to relocate a specific version?
Put one of the elasticsearch-spark dependencies into a subproject aa2 and relocate it. Then the other subproject aa1 can depend on aa2's shadow configuration.
// aa2/build.gradle
dependencies {
implementation 'org.elasticsearch:elasticsearch-spark-13_2.10:6.2.2'
}
shadowJar {
relocate "org.elasticsearch", "org.elasticsearch_v6_2_2"
}
// aa1/build.gradle
dependencies {
implementation 'org.elasticsearch:elasticsearch-spark-13_2.10:6.3.2'
implementation project(path: ':aa2', configuration: 'shadow')
}
You can now declare the same class in this way:
package com.github.chehsunliu.stackoverflow.q56332118;
import org.elasticsearch.spark.cfg.SparkSettings;
public class App {
public static void main(String[] args) {
System.out.println(SparkSettings.class);
System.out.println(org.elasticsearch_v6_2_2.spark.cfg.SparkSettings.class);
}
}
However, you should pay more attention to their transitive dependencies. You might also need to relocate them to make the direct dependencies work normally. Sometimes I will decompress the output JAR file and investigate these .class files to ensure the resolution correctness.
References
Minimal project: https://github.com/chehsunliu/stackoverflow/tree/main/gradle/q56332118
Decompiler https://java-decompiler.github.io/

Compile zip with gradle including libraries implemented by a sibling subproject

I have a java project made by some sibling modules. One of these modules is a library and I'm applying java-library plugin on it. All the other modules depend on it.
What I need to do is to automate the creation of a zip for each module, containing all the classes and all the dependencies needed for it to work (I'm deploying the zip as aws-lambda functions).
So far this is what I have achieved, but the resulting zip only contains module's classes. I thought that the problem might be the type of dependency I'm using (implementation) and I tried switching to the default one but gradle doesn't even success in building.
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task buildZip(type: Zip) {
from compileJava
from processResources
from configurations.runtime
}
dependencies {
implementation project(':utils')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
After some testing, I think I have the answer.
First: the order you declared the task and the dependencies is fine! It's OK to use a configuration before it's populated. I only say this because I indicated in my comments that it might be an issue.
Second, the issue here is the use of configurations.runtime. This does not extend implementation and api, so those dependencies are not included. runtime has been superseded by runtimeOnly, which hopefully makes the behaviour clear.
The following task definition should work:
task buildZip(type: Zip) {
from compileJava
from processResources
from configurations.runtimeClasspath
}

Remove dependency in Gradle

In my project I have to libraries that uses com.google.javascript:closure-compiler. Due to some jars problem I want to exclude one. To do this I wrote this code:
compile('com.bertramlabs.plugins:asset-pipeline-core:2.8.1') {
exclude group: 'com.google.javascript', module: 'closure-compiler'
}
When I run ./gradlew dependecies old jar seems to be removed.
My problem occurs when I run ./gradlew install and import my lib in another project, because in another project I have both jars. How can I remove it?
You could try adding a provided configuration as described here.
Once that is in place, try changing your code to something like:
provided('com.bertramlabs.plugins:asset-pipeline-core:2.8.1') {
exclude group: 'com.google.javascript', module: 'closure-compiler'
}

Categories