How to exclude source folder from jar file - java

How can I exclude a source folder from a jar file using Gradle Kotlin DSL?
The source sets are
sourceSets {
main {
java {
srcDirs("src/main/java")
srcDirs("src/util/java")
}
}
}
and I want to exclude src/util/java from Jars.
I tried
tasks {
withType<Jar> {
exclude("src/util/java/**")
}
}
but that does ot work because the string likely does not contain the source folder.
What is the best way to achieve this?

Try to filter in the compile task.
tasks.withType(JavaCompile) {
exclude '**/util/**/*.java'
}
but this is appropriate if you looking to exclude some specific files.
If the whole src folder (like util) is to be excluded, it would be better redefine the sourceSets.main.java to only src folders you need - for that module

Related

How to create a JAR file from gradle that contains the .java fields (source code) as well

I am using gradle to build some a uber jar file, this jar file has dependencies and I used this guide to create the task that I can run
My task is like this btw I am using kotlin
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
from(sourceSets.main.get().output)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE;
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
}})
What can I add to this task to add my .java files as well.
Also I don't really understand much of what is going on here like what actually is SourceSets?
Try adding this to your build.gradle.kts and see if it works:
tasks.named<ProcessResources>("processResources") {
from("src/main/java")
}
When your uber-JAR builds, it should contain the source files. Though this will also add the source files to any JAR. If that is not what you want, then you will need to configure something similar in the uber-JAR setup.

gradle compile multipe source

This is straight from Gradle 3.2.1 specifying_multiple_files. Why this does not work ? Why it cannot pick up files from ../shared/java during compilation. Compilation fails for sources in src/main/java depend on ../shared/src
The example is straight from Gradle
task compile(type: JavaCompile)
compile {
// Add some source directories use String paths
source 'src/main/java', 'src/main/groovy'
// Add a source directory using a File object
source file('../shared/java')
// Add some source directories using a closure
source { file('src/test/').listFiles() }
}
Unfortunately that page is written to show how you can work with files, but not necessarily how to configure the Java Compiler.
https://docs.gradle.org/current/userguide/java_plugin.html
That page will give you the details for what you are trying to solve. What you need is to instead just define the sourceSet.
sourceSets {
main {
java {
srcDirs = ['src/main/java', '../shared/java']
}
}
}

How do I specify an extra folder to be on the classpath for gradle's application plugin 'run' task?

I've successfully configured my gradle build script to create a zip distribution of my application with an extra 'config' folder at the root. This folder contains (at least right now) only one properties file in use by the application, and is on the classpath for the application.
What I'm looking for now, however, is a way to do the same with the 'run' task in the application plugin. When I try to run my application this way, (for testing), my program fails to run because of a class trying to access this properties file on the root of the classpath.
A bonus would be if I could get IntelliJ or Eclipse to also add this folder to its classpath just like the other folders (src/main/java, src/main/resources, ...) so I can run and debug my code from within the IDE without invoking a gradle task. I want to try to avoid as much as possible tying this code to any one IDE, so that when anybody needs to work on the project, they just need to import the build.gradle file and have the IDE make the appropriate config files it needs.
Here is my build.gradle file:
apply plugin: 'application'
mainClassName = "MainClass"
startScripts {
// Add config folder to classpath. Using workaround at
// https://discuss.gradle.org/t/classpath-in-application-plugin-is-building-always-relative-to-app-home-lib-directory/2012
classpath += files('src/dist/config')
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\config', '%APP_HOME%\\config')
unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/config', '$APP_HOME/config')
}
}
repositories {
...
}
dependencies {
...
}
Likely what needs to happen is that I need to have the /src/dist/config folder to be copied into the build directory and added to the classpath, or have its contents be copied into a folder that is already on the classpath.
I ended up taking Opal's suggestion as a hint, and came up with the following solution. I added the following to my build.gradle file:
task processConfig(type: Copy) {
from('src/main/config') {
include '**/*'
}
into 'build/config/main'
}
classes {
classes.dependsOn processConfig
}
run {
classpath += files('build/config/main')
}
Alternatively, a simpler approach would be to add a runtime dependency to my project as such:
dependencies {
...
runtime files('src/main/config')
}
I didn't end up doing it this way, however, because my distribution package ended up having .properties files in the lib folder... and I'm just picky that way.
As you can see in the docs run is a task of type JavaExec. So classpath for it can be modified. Try to add config folder to the classpath. See here.

How to Exclude directory and its contents in gradle war

I am using gradle war plugin, I am trying to exclude some directories inside WEB-INF directory while packing a war, but the excludes don't seem to work.
This is what I have
war {
webInf {
from 'src/main/config'
}
exclude('metadata/**')
}
Any solution on this ?
To exclude a folder named metadata contained in WEB-INF use the following code in your build.gradle file:
war.rootSpec.exclude("**/WEB-INF/metadata/")
or depending on your coding style you could also use:
war
{
rootSpec.exclude("**/WEB-INF/metadata/")
}
This link on the gradle discussion forum may also be of help
Are you looking for something like this http://gradle.1045684.n5.nabble.com/Exclude-properties-file-from-war-td3365147.html? If so only remember, that in recent versions of Gradle (I'm using 1.6) there's sourceSets.main.output.
It'd helpful if you could specify what metadata is and attach your project's layout.
One more thing: you may also like to:
//remove jars only for provided compile
classpath -= configurations.providedCompile
see this post and comments
war {
// remove classes from the classpath <<<<<<<
classpath = configurations.runtime
// add them in explicitly, with the filtering applied
webInf {
into('classes') {
from sourceSets.main.classes
exclude 'org/gradle/sample/excludeme/**'
}
}
}
sourceSets {
main {
src {
srcDir 'config'
exclude 'metadata/**'
}
}
}

Compile folder with .java files and Jar it

I'm trying to figure out compiling a folder with Java source files in it.
There's a folder structure (root folders are org.AppName.i18n and META-INF). Eventually I need to get a Jar with the same structure and .java's converted to .class'es. Any hints how to make it? I'm on Windows.
You can use Ant for this: http://ant.apache.org/
A build tool, like Gradle is perfect for this.
See the tutorial for java projects.
Create a build.gradle file, where you use the java plugin:
apply plugin: 'java'
You either need to move your java files to src/main/java, or specify the source folder in build.gradle, something like this:
sourceSets {
main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
Then run 'gradle jar'

Categories

Resources