I'm using the gradle shadow plugin in my java project.
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
I used the following code to get rid of all the dependencies I could.
shadowJar {
dependencies {
exclude(dependency("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT"))
exclude(dependency("org.jetbrains:annotations:22.0.0"))
exclude(dependency("com.comphenix.protocol:ProtocolLib:4.7.0"))
}
}
However, I've got all these extra files in my jar file. I want to remove everything except what's in the "eu" folder and the "plugin.yml". How can I do this?
Don't use shadowJar {} unless you have to, just change "implementation" to "compileOnly" for the dependencies you don't want in the jar
Related
I'm adding Checker Framework to an existing Java Gradle build. I have Checker integrated and raising issues and that's all working great. But now that I've added it, my build is no longer producing a .jar file as it once did.
Previously, I'd get a .jar at build/libs/myproject.jar. Now instead I see build/checkerframework and build/main but no build/libs and no .jar.
My build.gradle is below. Anyone attempted this before? Any success?
I guess I'd also accept an answer that shows how run Checker outside of the build, e.g. gradle check to run a build with Checker, and gradle build to produce a .jar. But I'd really prefer to have just a single build step if at all possible.
Thanks!
plugins {
id "org.checkerframework" version "0.5.18"
id 'application'
}
apply plugin: 'org.checkerframework'
checkerFramework {
checkers = [
'org.checkerframework.checker.nullness.NullnessChecker',
'org.checkerframework.checker.units.UnitsChecker'
]
}
repositories {
jcenter()
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// Embed all dependencies to create a "fat jar" as required for AWS deployment.
// Exclude 3rd-party signing files to prevent security errors at runtime
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
manifest {
attributes('Main-Class': 'client.CliMain')
}
}
If the build is passing, then adding -processor should not affect whether a .jar file is produced. (Unless you pass -proc:only or the like.)
If the build is failing, then Gradle won't build the jar files.
You said the Checker Framework is "raising issues", so maybe the build is failing.
I'm working on a java 8 gradle project in Eclipse (2019-09) including lombok. I would love to use mapstruct for converting models into dto and vice versa.
Mapstruct works great.
Problem:
I have some trouble that Eclipse creates and compiles the mapper implementation when saving the file.
The XYMapperImpl.java/class files are only created when using ./gradlew classes.
Building it with gradle is not really an option because it takes some time and does not really work in my workflow. I need to have the .class file at the end to mount the whole /bin folder into a docker container.
I'm using the Eclipse MapStruct plugin, however this is only for Code Completion and Quick Fixes but not for generating class files.
I've done some research:
How to get Eclipse to generate MapStruct Mappers using Gradle or How to properly integrate MapStruct with Eclipse? (Including Lombok java agent) and some others without any success.
Maybe I missed something?
My build.gradle file:
plugins {
id 'java'
id "net.ltgt.apt-eclipse" version "0.21"
id "net.ltgt.apt" version "0.21"
}
apply plugin: "java"
apply plugin: "net.ltgt.apt-eclipse"
apply plugin: "net.ltgt.apt"
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.10'
compile "org.mapstruct:mapstruct-jdk8:1.3.0.Final"
annotationProcessor "org.mapstruct:mapstruct-processor:1.3.0.Final" //Must be defined before the lombok annotationProcessor
annotationProcessor 'org.projectlombok:lombok:1.18.6'
}
I've created a simple demo project as an example.
I'm using Antlr in a simple Kotlin/Gradle project, and while my Gradle build is generating Antlr sources, they are not available for importing into the project.
As you can see (on the left), the classes (Lexer/Parser, etc.) are being generated. I have also configured this generated-src/antlr/main directory as a Source Root. Most questions I see list this as a solution, but I've already done it.
The issue persists after multiple rebuilds (both in IDEA and on the CLI), and following all the usual "Invalidate Cache and Restart" issues.
Further, the import issue is listed in the Gradle build on the CLI so it doesn't seem isolated to IDEA.
What am I missing here?
Here's the build.gradle file produced by IDEA when I was creating the project initially, and which IDEA is using for project/workspace synchronization.
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.50'
}
group 'com.craigotis'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
apply plugin: 'antlr'
dependencies {
antlr "org.antlr:antlr4:4.5"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Try adding this to your build.gradle:
sourceSets {
main.java.srcDirs += "${project.buildDir}/generated-src/antlr/main"
}
generateGrammarSource {
arguments += ["-visitor", "-package", "com.craigotis.sprint.core.antlr"]
outputDirectory = file("${project.buildDir}/generated-src/antlr/main/com/craigotis/sprint/core/antlr")
}
compileKotlin.dependsOn generateGrammarSource
Shouldn't it locate the compiled classes and not the sources? Do you see the antlr generated classes in the target directory?
Try this: first build the project without referencing or using any ANTLR generated classes, and only after the build is successful, then add the code that references them.
(In other words, what I think that happens, is that your ANTLR sources are compiled after the code that references them. They never have a chance to compile because build fails before)
Also if this is really the case, you can solve it also by splitting into two artifacts and make sure the ANTLR one is built before the one with the code that uses it
Try to add generated sources in idea module like this post from Daniel Dekany here:
apply plugin: "idea"
...
sourceSets.main.java.srcDir new File(buildDir, 'generated/javacc')
idea {
module {
// Marks the already(!) added srcDir as "generated"
generatedSourceDirs += file('build/generated/javacc')
}
}
(this is using gradle 2.4)
For one of my projects, split into several submodules, I use the shadow plugin which works very well for my needs; it has a main, and as recommended by the plugin's README, I use the application plugin in conjuction with it so that the Main-Class is generated in the manifest, all works well.
Now, this is a SonarQube plugin project, and I also use (successfully!) the gradle sonar packagin plugin. And what this plugin does is, when you ./gradlew build, generate the sonar plugin instead of the "regular" jar.
I wish to do the same for my subproject here, except that I want it to generate only the shadow jar plugin instead of the "regular" plugin... Right now I generate both using this simple file:
buildscript {
repositories {
jcenter();
}
dependencies {
classpath(group: "com.github.jengelman.gradle.plugins",
name:"shadow", version:"1.2.1");
}
}
apply(plugin: "application");
apply(plugin: "com.github.johnrengelman.shadow");
dependencies {
// whatever
}
mainClassName = //whatever
artifacts {
shadowJar;
}
// Here is the hack...
build.dependsOn(shadowJar);
How do I modify this file so that only the shadow jar is generated and not the regular jar?
You could disable the jar task by adding the following lines to your gradle script:
// Disable the 'jar' task
jar.enabled = false
So, when executing the gradle script, it will show
:jar SKIPPED
If you wish to configure all sub-projects, then you can add the following into your root build.gradle
subprojects {
// Disable the 'jar' task
tasks.jar.enabled = false
}
I am attempting to make a test application using Gradle and Java that uses several libraries that use the Java Service Provider interface. I think this means that I need to modify META-INF but I am not really sure how to do this.
The error that I get is An SPI class of type org.apache.lucene.codecs.codec with nameLucene50does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classpath supports the following names [ SimpleText]
I think that I need to get the SPI information into META-INF but I am not sure how to do this with Gradle.
Specifically, I am trying to use Lucene and Hadoop jars in the following build file:
apply plugin: 'java'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile group:'org.apache.lucene', name:'lucene-core', version:'5.0.0'
compile group:'org.apache.lucene', name:'lucene-queryparser', version:'5.0.0'
compile group:'org.apache.lucene', name:'lucene-analyzers-common', version:'5.0.0'
compile group:'org.apache.lucene', name:'lucene-facet', version:'5.0.0'
compile group:'org.apache.lucene', name:'lucene-codecs', version:'5.0.0'
compile group:'org.apache.hadoop', name:'hadoop-hdfs', version:'2.6.0'
compile group:'org.apache.hadoop', name:'hadoop-core', version:'1.2.1'
compile group:'org.apache.hadoop', name:'hadoop-common', version:'2.6.0'
}
jar
{
from {configurations.compile.collect {it.isDirectory() ?it:zipTree(it) }}
manifest
{
attributes 'Main-Class': 'LuceneTest'
}
}
Both the lucene-core and lucene-codecs libraries provide org.apache.lucene.codecs.Codec implementations, so they both have a META-INF/services/org.apache.lucene.codecs.Codec service file. When you merge all your dependencies, both files are added to the jar file, but Lucene only sees the lucene-codecs one. You could merge the service files manually in the jar task, as in this post, which basically finds all the service files and combines them. The easier solution is probably to use something like the Gradle Shadow plugin.
If you add this to build.gradle, using the shadowJar task instead of the jar task should do what you want.
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.1'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
shadowJar {
mergeServiceFiles()
}