error occur after setting class outputdir in build.gradle - gradle

I want to setting a new dir for compiled java class file when use gradle.
But it never work.
apply plugin: 'java'
//apply plugin: 'war'
sourceSets {
main {
java {
srcDirs = ['src/']
outputDir = 'WebContent/WEB-INF/classes/'
}
resources {
srcDirs = [ 'src/' ]
}
}
}

For gradle > 4. Note for type of the value for outputDir property. It should be File. Not String like in your question's code:
sourceSets {
main {
output.resourcesDir = file('out/res')
java.outputDir = file('out/bin')
}
}
see
https://docs.gradle.org/current/javadoc/org/gradle/api/file/SourceDirectorySet.html#setOutputDir(java.io.File)
and
https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/SourceSetOutput.html
Before version 4 gradle was using single output directory for different jvm languages.
So, for gradle < 4:
sourceSets {
main {
output.resourcesDir = file('out/res')
output.classesDir = file('out/bin')
}
}
see https://docs.gradle.org/3.3/javadoc/org/gradle/api/tasks/SourceSetOutput.html.

Related

Problem with import and using SwingFXUtils in gradle project

I'm very new to gradle and javaFx.
I'm working on a gradle project (not mine) and I need to import SwingFXUtils to make some operations.
Importing it into the java file:
import javafx.embed.swing.SwingFXUtils;
cause the error: "Cannot resolve symbold SwingFXUtils".
I tried to add it to java-modules.java file
requires javafx.embed.swing;
but it is not recognized and I'm having the error message: "Module not found:javafx.embed.swing"
then, I updated my build.gradle for the missing module: like this:
plugins {
id 'application'
id 'org.beryx.jlink' version '2.19.0'
id 'com.google.osdetector' version '1.6.2'
}
repositories {
flatDir {
dirs "libs"
}
jcenter()
maven { url "https://maven.geotoolkit.org/" }
mavenCentral()
}
ext {
toolName = 'my project'
toolVersion = '1.0.0'
mainModuleName = 'com.myproject'
mainClassName = 'com.myproject.DcTool'
jfxVersion = '14.0.1'
pdfboxVersion = '2.0.27'
platform = osdetector.os == 'osx'?'mac':osdetector.os =='windows'?'win':osdetector.os
fixWayland = platform == 'mac' ? '' : platform == 'win' ? '' : '-Djdk.gtk.version=2'
}
version = "$toolVersion"
// Project dependencies.
dependencies {
compileOnly "org.jetbrains:annotations:19.0.0"
implementation "org.openjfx:javafx-base:$jfxVersion:$platform"
implementation "org.openjfx:javafx-graphics:$jfxVersion:$platform"
implementation "org.openjfx:javafx-controls:$jfxVersion:$platform"
implementation "org.openjfx:javafx-fxml:$jfxVersion:$platform"
implementation "com.fazecast:jSerialComm:2.6.2"
implementation "ch.qos.logback:logback-classic:1.3.0-alpha4"
implementation "de.skuzzle:semantic-version:2.1.0"
implementation "info.picocli:picocli:4.3.2"
annotationProcessor "info.picocli:picocli-codegen:4.3.2"
implementation "org.apache.pdfbox:pdfbox:${pdfboxVersion}"
implementation "org.apache.pdfbox:preflight:${pdfboxVersion}"
implementation "com.fasterxml.jackson.jr:jackson-jr-objects:2.11.0"
implementation group: 'org.openjfx', name: 'javafx-swing', version: '11-ea+24'
}
java {
modularity.inferModulePath = true
}
application {
mainModule.set("$mainModuleName")
mainClass.set("$mainClassName")
applicationDefaultJvmArgs = [
"-Dfile.encoding=UTF-8",
]
}
tasks.withType(JavaCompile) {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath
]
classpath = files()
}
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_14
options.encoding = "UTF-8"
options.javaModuleVersion.set("$toolVersion")
options.javaModuleMainClass.set("$mainClassName")
}
run {
doFirst {
jvmArgs = [
"--module-path", classpath.asPath,
'--patch-module', "$mainModuleName=" + files(sourceSets.main.output.resourcesDir).asPath,
"--module", "$mainModuleName/$mainClassName"
]
jvmArgs += application.applicationDefaultJvmArgs
classpath = files()
}
doLast {
args = []
}
}
jar {
manifest {
attributes(
'Main-Class': "$mainClassName"
)
}
}
JAVA_HOME=$(/usr/libexec/java_home)
jlink {
moduleName.set("$mainModuleName")
mainClass.set("$mainClassName")
addOptions(
'--bind-services',
'--no-header-files',
'--no-man-pages',
'--strip-debug',
'--compress', '2')
launcher {
name = "$toolName"
jvmArgs += application.applicationDefaultJvmArgs
}
}
I also tried to download jars of SwingFx and add them in a "libs" folder into the project but I have no result and compiler doesn't find modules I need.
What can I do to make it find this module?
Thank you for your help.

Jacoco | Gradle | Exclude from aggregated modules

I have a Java-Gradle project that has multiple modules, meaning multiple build.gradle files and multiple test folders.
I have a main build.gralde and I succeeded to aggregate all the multi jacoco reports to one main report using the configuration below at my main build.gradle.
but now I want to exclude some of packages / classes from the aggregated modules.
How do I do that?
you can see also my tries below
apply plugin: 'jacoco'
apply plugin: 'java'
def otherProjects = [':module1', ':module2']
otherProjects.each {
// ensure other projects evaluated first so sourceSets are configured
evaluationDependsOn it
}
jacoco {
toolVersion = "0.8.4"
reportsDir = file("$buildDir/jacoco")
}
jacocoTestReport {
FileTree sourceTree = files().asFileTree
FileTree classTree = files().asFileTree
otherProjects.each {
sourceTree += project(it).sourceSets.main.allJava
classTree += project(it).sourceSets.main.output.asFileTree
}
additionalSourceDirs = sourceTree
additionalClassDirs = classTree
reports {
html.enabled true
html.destination file("${buildDir}/jacocoHtml")
}
// try 1
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: '/com/packege_from_module1/**')
})
}
// try 2
afterEvaluate {
additionalSourceDirs = files(additionalSourceDirs.files.collect {
sourceTree(dir: it, exclude: 'com/packege_from__module1/**')
})
}
// try 3
afterEvaluate {
additionalSourceDirs = files(additionalSourceDirs.files.collect {
classTree(dir: it, exclude: 'com/packege_from__module1/**')
})
}
}
Given
a/src/main/java/A.java
class A {
}
a/src/test/java/ATest.java
import org.junit.Test;
public class ATest {
#Test
public void a() {
}
}
b/src/main/java/B.java
class B {
}
b/src/test/java/BTest.java
import org.junit.Test;
public class BTest {
#Test
public void b() {
}
}
settings.gradle
rootProject.name = 'example'
include 'a'
include 'b'
and build.gradle
allprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.3"
}
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
}
task jacocoAggregateReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
dependsOn = subprojects.test
sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories = files(subprojects.sourceSets.main.output)
executionData = files(subprojects.jacocoTestReport.executionData)
}
using Gradle 4.10.3 or 5.4.1 execution of gradle jacocoAggregateReport will produce following report in directory build/reports/jacoco/jacocoAggregateReport/html/index.html
Now let's exclude class file A.class from report by changing
classDirectories = files(subprojects.sourceSets.main.output)
in build.gradle on
classDirectories = files(subprojects.sourceSets.main.output).asFileTree.matching {
exclude 'A.class'
}
and execution of gradle jacocoAggregateReport will produce
Your attempts did not worked, because report is constructed for all class files from both classDirectories and additionalClassDirs - no exclusion from classDirectories and additionalClassDirs in attempts 2 and 3, no exclusion from additionalClassDirs in attempt 1.

How to access list of values from gradle.properties to build.gradle

I have the following gradle.properties
version=2.1
paths=['/home/desk/hie', '/home/mydesk1/hai1', '/home/mydesk2/hai2']
sources=['/src/path/impl', '/src/path/src']
I need to access these paths and sources form gradle.properties to build.gradle
sourceSets {
main {
java {
srcDir=${paths}
}
}
}
but ${paths} is not working.
Can you someone help to get out of this issue and how to use those list in build.gradle file
Consider the following, which converts the paths String to an Iterable (that is, a List):
apply plugin: 'java'
def pathsList = Eval.me(project.ext.paths)
sourceSets {
main {
java {
srcDirs = pathsList
}
}
}
You can share the value using gradle object:
// settings.gradle
gradle.ext {
paths = ['/home/desk/hie', '/home/mydesk1/hai1', '/home/mydesk2/hai2']
}
// build.gradle
sourceSets {
main {
java {
srcDir = paths
}
}
}

Get SourceSet full path string or path directory

I would like to know if there is a property that returns a SourceSet full path string or file directory, something like ${project.projectDir}/src/${sourceSet.name}.
apply plugin: 'java'
sourceSets {
foo {
java {
srcDir 'example/dir/java'
}
}
}
// For example, this could return pathToProject/src/example/dir
println sourceSets.foo.srcDir
Try this:
productFlavors {
admin { }
customer { }
}
sourceSets {
main {
java.srcDirs = ['src/main']
//other typical sourceSets stuff
}
admin.java.srcDirs = ['src/admin']
customer.java.srcDirs = ['src/customer']
}

Gradle build - Maven Modello

I have a Maven build which uses maven-modello (1.4) to generate Java classes/XSD's etc. from an description file (modello file). I'm searching for a possible solution in Gradle to solve the same problem.
I haven't test it, but can something like that do the trick:
import org.codehaus.modello.Modello
// Dependencies
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.codehaus.modello:modello-maven-plugin:1.5'
}
}
// Execution
task modello << {
buildDir.mkdirs()
file("$projectDir/models").eachFile { modelFile ->
if (modelFile.name.endsWith('.mdo')) {
new Modello().generate(modelFile.newReader(), generator, parameters)
}
}
}
// Configuration
modello.ext {
generator = 'java'
parameters = new Properties()
parameters.'modello.output.directory' = buildDir.absoluteFile
parameters.'modello.version' = '1.5'
parameters.'modello.package.with.version' = false
parameters.'modello.output.useJava5' = true
parameters.'modello.output.encoding' = 'UTF-8'
}

Resources