Removing "src" folder from project - java

Default structure of Gradle project created by IntelliJ IDEA is:
module/src/main/java
module/src/main/resources
module/src/test/java
module/src/test/resources
I would like to cut src folder, so I will be left with:
module/main/java
module/main/resources
module/test/java
module/test/resources
I have been removing them from every possible corner in project structure -> modules. Removed them by clicking on them from IJ, but IJ keeps creating them. I have even replaced all strings in whole project folder "/src" to "" but it works until next Gradle refresh + rebuild.
How to get rid of that folder?

You can change default project layout and remove src folder from it's paths, by changing source sets properties as follows:
sourceSets {
main {
java {
srcDirs = ['main/java']
}
resources {
srcDirs = ['main/resources']
}
}
test {
java {
srcDirs = ['test/java']
}
resources {
srcDirs = ['test/resources']
}
}
}
You can read about it in the official user guide.

You can't get rid of of neither 'main' or 'test' source sets. Only thing you can do is mark them as excluded.

Related

Gradle : Exclude certain files from packaging (jar) using annotations

I am looking for a solution to exclude certain files marked with a particular annotation to be packaged in jar (can be compiled but not part of jar created).
I have tried the following steps
Create a ClassLoader using : sourceSets.main.output + configurations.runtime
Check recursively within the compiled classes, use ClassLoader.loadClass to load the class and check if annotation is present (Class.isAnnotationPresent)
Any pointers would be helpful.
I was able to implement this long time back but forgot I had posted the question here.
The solution I used to was actually quite simple -
Using gradle jar task -
jar {
excludes = excludedFiles(sourceSets.main.allSource.files)
baseName = artifactName
version = artifactVersion
}
And define the excludedFiles function to look up the files in the source directory provided -
def excludedFiles(Collection<File> files) {
List<String> classes = new ArrayList<>()
files.each { file ->
if (file.isDirectory()) {
excludedFiles(Arrays.asList(file.listFiles()))
}
else {
if (file.text.contains("#YourAnnotation") && file.text.contains("import foo.bar.YourAnnotation")) {
classes += getClassName(file.absolutePath)
}
}
}
return classes
}
Hope this helps.

Include specific classes in jar archive in gradle build

I have a newbie question on a gradle java project. I wish to configure the output artifact jar of this to only include certain classes from my project. I have the desired classes to be included in a file say classes.txt that is generated as a part of a separate task. How should one configure the jar task of the gradle build so that does this. This is what I have tried so far:
jar {
// reset actions
actions = []
copy {
def dependentClasses = file("classes.txt")
if (dependentClasses.exists()) {
dependentClasses.eachLine { include it }
}
from sourceSets.main.output
includeEmptyDirs = false
into "build/tmp/jar" //Some temporary location
}
// How to zip the contents?
}
I am sorry for the possibly naive question, but I haven't had luck with the other solutions seen.
Thanks!
It can be done in the following way:
apply plugin: 'java'
jar {
def excluded = project.file('classes.txt').readLines()
exclude { f ->
excluded.any { e -> f.path.replaceAll(File.separator, '.').contains(e) }
}
}
Demo can be found here, Lol1.class will be excluded.
Using Opal's answer, here is the answer for the include of files
apply plugin: 'java'
jar {
def included = project.file('classes.txt').readLines()
include { f ->
included.any { i -> f.isDirectory() ? true : f.path.replaceAll(File.separator, '.').contains(i) }
}
}
The slight trick was that the FileTreeElement f being passed to the closure also has the package directory passed and if that happens to return false, the subtree below is not traversed and hence the check on it being a directory to return true to enable processing of the subtree.
Demo for solution is here and Lol1.class will be included. Thanks for the help, Opal!

Include additional Sources for recompile in gwtSuperDev Task

I use the GWT Gradle Plugin. I have a second project which I included in my main project with:
dependencies {
compile(project(':core-project')) {
transitive = false
}
}
I have also created a settings.gradle which contains
includeFlat 'core-project'
The core-project is a library project. When I run gradle gwtSuperDev and change files in my main project recompile takes place but not if I change files in my core-project.
How can I make that the core-project sources are also recompiled in SuperDevMode when they have changed?
Depending on how your gradle dependencies are organized, you can simply add their source-files to the gwt section of your build.gradle file.
I have something like this in my build file:
gwt {
gwtVersion = '2.7.0'
if (System.getProperty('devmode') != null) {
modules = ['dk.logiva.invoice.autoaccount.gwt.AutoAccountAdminDev']
compiler {
style = 'PRETTY';
strict = true;
}
// include sources from inherited module
src += project(':invoice:gwt:gwt-common').files('src/main/java')
} else {
modules = ['dk.logiva.invoice.autoaccount.gwt.AutoAccountAdmin']
}
devWar = file("${buildDir}/war")
maxHeapSize = "3G"
}
...
dependencies {
compile project(':invoice:gwt:gwt-common')
...
}

Create many sourceSets in Gradle

I have a project in which there are folder with different zip archives. I want to extract them and for each to make some checks. But want reports to be separated. For example pmd check.
So to be separated files from each zip file has to be but in different sourseSets and when call
gradle check
For each of the dirs to generate reports with the checks.
Input files:
sources1.zip,sources2.zip,sources3.zip,sources4.zip
Each of the zip contains separate project.
for example:
src/main/java/sources1/Example.java
src/main/java/sources2/App.java
....
When run checks to be generated
build/reports/pmd/sources1SourceSet.html
build/reports/pmd/sources2SourceSet.html
build/reports/pmd/sources3SourceSet.html
build/reports/pmd/sources4SourceSet.html
So I want to declare this different sourceSets according the name of the zips and not to do it manually, but don't know how.
sourceSets {
sourcest1{
java {
srcDirs file("...")
}
}
}
Is there are another easy way to separated checks without creating new sourceSets for each zip?
one way to start you could create dedicated tasks for each zip. This can be done in a loop:
["source1", "source2", "source3"].each{ sourceName ->
tasks.create("pmd${sourceName}", Pmd){
reports.html.reportsDir = "$buildDir/reports/pmd${sourceName}"
source = zipTree("file/to/$sourceName.zip")
...
}
}
cheers

Gradle: Create a jar with no classes, only resources

I'm trying to create a group of four jars with the following pattern (each jar has its own project. helpRootDir is shared between all four jars. If somebody knows of a way to make one task that does all four, that'd be awesome)
def helpRootDir = 'runtime/datafiles/help/'
project(':schedwinclihelp') {
def helpDir = 'schedwincli'
//Include no classes. This is strictly a resource jar
sourceSets.main.java {
exclude 'com/**'
}
jar {
from '${helpRootDir}/${helpDir}'
include '**/*.*'
}
}
Anyway as you can see from the above, I want no classes in the jar, and that's working. Unfortunately, all I'm actually getting in the jar is a MANIFEST.MF file. None of the files in the jar definition are getting added. I want the full file tree in ${helpRootDir}/${helpDir} added to the root of the jar. How do I accomplish this?
Figured out I was referencing my variables incorrectly.
The correct syntax is:
def helpRootDir = 'runtime/datafiles/help/'
project(':schedwinclihelp') {
def helpDir = 'schedwincli'
//Include no classes. This is strictly a resource jar
sourceSets.main {
java {
exclude 'com/**'
}
resources {
srcDir helpRootDir + '/' + helpDir
}
}
}
Note srcDir helpRootDir + '/' + helpDir rather than '${helpRootDir}/${helpDir}'. Also, I just made my help dir a resource dir and let the java plugin do its thing automatically.
The following task will create a JAR file named resources.jar with main resource files only (those are placed under src/main/resoures directory).
Kotlin DSL:
tasks {
task<Jar>("resourcesJar") {
from(sourceSets["main"].resources)
archiveFileName.set("resources.jar")
}
}
Groovy DSL:
tasks.create("resourcesJar", Jar.class) {
from sourceSets.main.resources
archiveFileName = "resources.jar"
}

Categories

Resources