Gradle: create .jar from .class files and include it in EAR_dir/lib when EAR is built - gradle

You'd think this'd be easy enough. Gradle/Maven were designed specifically to get rid of build nightmares. And yet... I have scoured the web, including SO. I would prefer to be using Maven but alas this is not in my control.
My master build.gradle file looks like this:
buildscript {
repositories {
maven { url "https://aaa.com/xxxx/aaa-mvn" }
}
dependencies {
{redacted}
classpath "com.aaa.plugin.gradle:module-plugin:1.+"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.4"
}
}
apply plugin: 'maven-publish'
apply plugin: {redacted}
apply plugin: {redacted}
apply plugin: 'build-defaults'
apply plugin: 'module-plugin'
apply plugin: 'war'
description = 'xxxxxx'
defaultTasks 'build','install'
dependencies {
jbossModule(group: 'com.aaa.bbbb', name: 'inf-jdbc', version: '3.0.2')
}
artifactory {
publish {
repoKey=version.endsWith("SNAPSHOT") ? 'aaa-mvn-dist-snapshots' : 'aaa-mvn-dist'
defaults {
publications('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact project(':xxxEAR').ear
}
}
}
repositories {
maven { url "https://xxxx.aaa.com/artifactory/aaa-mvn" }
}
This is gradle.build for my .war. I have a .war inside an .ear.
description = 'Pricing'
buildscript {
repositories {
maven { url "https://xxx.aaa.com/artifactory/aaa-mvn" }
}
dependencies {
classpath "com.aaa.plugin.gradle:ucd-publish-plugin:1.+"
classpath "com.aaa.plugin.gradle:build-defaults-plugin:1.+"
classpath {redacted}
classpath {redacted}
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jacoco'
apply plugin: {redacted}
apply plugin: 'ucd-publish'
apply plugin: 'build-defaults'
apply plugin: {redacted}
apply plugin: 'maven'
apply plugin: 'maven-publish'
repositories {
maven { url "https://xxxx.aaa.com/artifactory/aaa-mvn" }
}
war {
archiveName 'xxxx.war'
}
dependencies {
compile {redacted}
compileOnly group: 'javax', name: 'javaee-api', version:'7.0'
testCompile "junit:junit:4.12"
compile 'io.swagger:swagger-annotations:1.5.10'
compile(group: 'com.aaa.inf', name: 'inf-jdbc', version: '3.0.2', classifier: 'sources')
compile(group: 'com.aaa.inf', name: 'inf-throttle', version: '3.0.1')
compile(group: 'com.ibm.db2', name: 'db2jcc', version: '3.64.133')
compile {redacted}
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.0
}
}
}
}
The build.gradle file for my .ear is:
description = 'xxxxx'
buildscript {
repositories {
maven { url "https://xxxx.aaa.com/artifactory/aaa-mvn" }
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.5"
classpath "com.aaa.plugin.gradle:build-defaults-plugin:1.+"
classpath "com.aaa.plugin.gradle:ucd-publish-plugin:1.+"
classpath "com.aaa.plugin.gradle:openapi-generator-gradle-plugin:1.+"
}
}
apply plugin: 'ear'
apply plugin: 'maven'
apply from: 'build.local-jboss.gradle'
apply plugin: 'maven-publish'
apply plugin: {redacted}
apply plugin: 'ucd-publish'
apply plugin: 'build-defaults'
apply plugin: {redacted}
configurations{
gen
}
dependencies {
deploy project (path: ":pricingWAR", configuration: 'archives')
earlib 'com.aaa.inf:inf-api-auth:1.4.+'
earlib {redacted}
earlib group: 'javax.security.enterprise', name: 'javax.security.enterprise-api', version: '1.0'
earlib (group: {redacted}) {
exclude group: 'javax.security.dddddd'
}
gen "io.swagger:swagger-codegen:2.2.2"
}
artifacts{
archives ear
}
repositories {
maven {
url "https://xxxx.aaa.com/artifactory/aaa-mvn"
}
}
task cleanVolumes(type: Delete) {
delete fileTree(dir: "./volumes/deployments/")
}
task copyEar(type: Copy) {
tasks.cleanVolumes.execute()
from "build/libs"
into "${project.projectDir}/volumes/deployments"
fileMode = 0644
}
build.finalizedBy(generateSwagger)
swaggerConfig {
archive = ear.archivePath
outputFormat = "JSON"
outputPath = project.buildDir.toString() + "/swagger"
}
Just looking to do this: .jar up the .class files compiled in my business classes and add them to EAR_file\lib.
I've gone so far as to try to use Gradle's native Groovy nature to write code that builds the .jar and moves it into EAR_file\lib. This of course is ridiculous. And worse still, it does not quite work. Paired with auto-deploy there are timing issues.
Some of you no doubt are wincing.
To save me from such insanity, please let me know what you know about this. I have of course tried various permutations of:
apply plugin: 'java'
...
jar {
...
}
to no avail.
Thank you in advance.

Finally have it working after helpful input from coworkers. For others' benefit here is the whole thing it ended up as:
In build.gradle in the EAR project level:
// My project is named: xxx-svc and the .war subproject is named pricing-theAPI
dependencies {
deploy project (path: ":xxx-theAPI", configuration: 'archives') // This was here already
earlib project(path: ":xxx-theAPI", configuration: 'customJar') // This is new
}
In build.gradle in the WAR project level:
configurations {
customJar
}
task doJar(dependsOn:classes, type: Jar){
from sourceSets.main.output
include 'com/xyz/xxxxx/yyyyyy/zzzz/**' // See Note 1
}
artifacts {
customJar doJar
}
Note 1: I added this modifier b/c in my case I needed only the .class files at the given location in the .jar and in fact had to exclude all others. Note the Gradle docs have the modifier "include" misidentified. They call it "includes" but in fact it is "include".

Related

How do I resolve the "Cannot assign 'String' to 'Publication'" Gradle errors in IntelliJ?

I'm using Gradle 6.7 for a project and I'm using IntelliJ. When writing Gradle files or when running inspections, I keep getting this error.
"Cannot assign 'String' to 'Publication'".
Based on the Gradle docs and all the examples, it seems as though my configuration is correct but I'm unable to resolve these warnings.
import org.gradle.api.tasks.testing.logging.TestLogEvent
buildscript {
buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
jcenter()
}
dependencies {
classpath "org.github.ngbinh.scalastyle:gradle-scalastyle-plugin_2.11:1.0.1"
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.8"
classpath "net.ltgt.gradle:gradle-nullaway-plugin:0.2"
classpath "gradle.plugin.com.dorongold.plugins:task-tree:1.5"
}
}
}
apply plugin: 'java'
apply plugin: 'scalaStyle'
apply plugin: 'scala'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'net.ltgt.errorprone'
apply plugin: 'net.ltgt.nullaway'
apply plugin: "com.dorongold.task-tree"
apply plugin: "jacoco"
group 'com.mridang'
version '0.0.2'
...
...
publishing {
repositories {
maven {
name = "github"
url = uri("https://maven.pkg.github.com/mridang/myrepo")
credentials(PasswordCredentials)
}
}
publications {
maven(MavenPublication) {
from components.java
artifact sourcesJar
artifact scaladocJar
pom {
name = 'myproject'
description = 'Foo'
url = 'https://github.com/mridang/myrepo'
issueManagement {
system = "Github"
url = "https://github.com/mridang/myrepo/issues"
}
}
}
}
}
Here's a screenshot that better illustrates the issue:
It is false positive inspection warning. You can vote for this issue in tracker: IDEA-162281.

jUnit 5 HTML Report using Gradle 4.6 [duplicate]

This question already has an answer here:
Configuration for Gradle 4.7 to generate the HTML report for JUnit 5 tests
(1 answer)
Closed 4 years ago.
I am trying to generate HTML report of JUnit 5 test case.
Here is my Gradle.build file
buildscript {
ext {
springBootVersion = '2.0.0.M3'
}
repositories {
maven { url 'https://zz-artifactory.zzzz.com/artifactory/maven' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.2.0-M1'
classpath "io.spring.gradle:dependency-management-plugin:1.0.0.RC1"
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: "io.spring.dependency-management"
apply from: "CodeCoverage.gradle"
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
compile('org.mybatis.spring.boot:mybatis-spring-boot:1.3.2')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter-test:1.3.2')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-aop')
compile("org.junit.jupiter:junit-jupiter-api:5.2.0-M1")
compile('org.junit.jupiter:junit-jupiter-migrationsupport:5.0.2')
compile("org.junit.jupiter:junit-jupiter-engine:5.2.0-M1")
compile("junit:junit:4.12")
compile("org.junit.vintage:junit-vintage-engine:5.2.0-M1")
compile(group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.2.0-M1')
compile(group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.2.0-M1')
testCompile('com.h2database:h2:1.4.196')
testCompile('org.mockito:mockito-all:2.0.2-beta')
testCompile('org.mockito:mockito-core:2.8.9')
}
test {
useJUnitPlatform()
}
junitPlatform {
// platformVersion '1.2.0-M1'
filters {
engines {
// include 'junit-jupiter', 'junit-vintage'
// exclude 'custom-engine'
}
tags {
include 'Smoke'
//exclude 'slow'
}
}
}
subprojects {
apply plugin: 'java'
test {
reports.html.enabled = false
}
}
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports123/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}
I have used testReport as per
Gradle docs Links
When i run the Gradle task, It runs all the test case, generate XML reprts, but doesnot generate HTML report
In the build log, i see
:testReport NO-SOURCE
any suggestion why it says NO-SOURCE ??
Thanks
You are doing one thing too many. You should either use 'org.junit.platform.gradle.plugin' or test { useJUnitPlatform } but not both.
The standard gradle (html) test reporting will only work with the latter.

Gradle: package multi-project into a single jar

I have a gradle multi-project and want to create a single jar (library) containing all the classes of my subprojects and external dependencies.
I have the following project structure. Each project has its own 3rd party dependencies. Common dependencies are included in the root project. The two modules A and B are dependent on the core.
+ root-project (only build.gradle and settings.gradle)
- core (src/main/java, src/main/resources, ..)
- module-A (src/main/java, src/main/resources, ..)
- module-B (src/main/java, src/main/resources, ..)
To export a single jar, I added the following task to the build.gradle of the root project:
apply plugin: "java"
subprojects.each { subproject -> evaluationDependsOn(subproject.path)}
task allJar(type: Jar, dependsOn: subprojects.jar) {
baseName = 'multiproject-test'
subprojects.each { subproject ->
from subproject.configurations.archives.allArtifacts.files.collect {
zipTree(it)
}
}
}
artifacts {
archives allJar
}
This approach works, but does only collect the project source files. The 3rd party dependencies are ignored. So I tried out the Shadow Plugin (http://imperceptiblethoughts.com/shadow/) which should also include external dependencies.
Unfortunately the plugin does not collect anything at all. This is most probably due to missing dependencies between the root project and its sub projects. How can I tell the shadow plugin, that it should collect the sources of the subprojects? Or is there a better approach to export a single library out of multiple projects?
complete build.gradle using the shadow plugin:
/****************************************
* instructions for all projects
****************************************/
allprojects {
apply plugin: 'idea'
apply plugin: 'eclipse'
group = 'com.test.multi-project'
version = '1.0'
}
/****************************************
* instructions for each sub project
****************************************/
subprojects {
apply plugin: "java"
sourceCompatibility = 1.9
targetCompatibility = 1.9
repositories {
mavenCentral()
}
dependencies {
compile "org.slf4j:slf4j-api:1+"
compile "ch.qos.logback:logback-core:1+"
compile "ch.qos.logback:logback-classic:1+"
testCompile "junit:junit:4+"
}
}
/****************************************
* Single jar out of all sub projects
****************************************/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
shadowJar {
baseName = 'multiproject-test'
}
The submodules are included in the settings.gradle of the root project
rootProject.name = 'myproject-root'
// submodules
include ":core"
include ":module-A"
include ":module-B"
Thanks for your help!
I solve my problem with the solution explained here: https://discuss.gradle.org/t/how-to-get-gradle-install-to-actually-bundle-all-project-subproject-classes-resources-etc/12070/4
My build.gradle looks now like this:
/****************************************
* instructions for all projects
****************************************/
allprojects {
apply plugin: 'idea'
apply plugin: 'java'
repositories {
mavenCentral()
}
group = 'com.test.multiproject'
version = '1.0'
sourceCompatibility = 1.9
targetCompatibility = 1.9
}
/****************************************
* instructions for each sub project
****************************************/
subprojects {
// common dependencies
dependencies {
compile "org.slf4j:slf4j-api:1+"
compile "ch.qos.logback:logback-core:1+"
compile "ch.qos.logback:logback-classic:1+"
testCompile "junit:junit:4+"
}
}
/****************************************
* Single library jar containing all sub projects and 3rd party dependencies
****************************************/
configurations {
childJars
}
dependencies {
subprojects.each {
childJars project(it.path)
}
}
jar {
dependsOn configurations.childJars
from { configurations.childJars.collect { zipTree(it) } }
}
How about getting all runtime libs while building jar itself
jar {
archiveName 'Some.jar'
manifest {
attributes 'Implementation-Title': 'Some',
'Plugin-Class': 'main'
}
from {configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it)}}
}
What about something simple like that :
task fatJar(type: Jar) {
subprojects.each { subproject ->
from subproject.configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}

One Gradle Thymeleaf project reloads on resource change, the other relaunches

EDIT: I've narrowed down the behavior a bit, not sure if there's a way to accomplish what I need.
Turns out, my app has the Thymeleaf templates one level down in the source tree:
src/main/resources/tools-server/templates
And I set this in my tools-server.yml file that gets explicitly loaded at application launch. Removing that specification from my configuration, and moving the templates directory up one level to
src/main/resources/templates
Allows spring-boot-devtools to simply reload the template without restarting the app. I think I’ll file a bug with the project, unless there’s a way around it.
I'm still getting the hang of Spring Boot, so bear with me. I've created two projects over the last few months, each starting from different examples found online.
With respect to reloading Thymeleaf templates, the first project does it neatly when they change, issuing two log messages when a template changes, and nothing more. The other does a complete stop and restart of the application, which causes problems because it re-creates the temporary security password, among other things (it also takes longer).
The two gradle.build files are nearly identical, with slightly different dependencies. I'm not sure if those are the differences causing the different behavior.
The working one:
buildscript
{
ext
{
springBootVersion = "1.4.3.RELEASE"
}
repositories
{
mavenCentral()
}
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath group: "com.layer", name: "gradle-git-repo-plugin", version: "2.0.2"
}
}
apply plugin: "git-repo"
apply plugin: "java"
apply plugin: "maven"
apply plugin: "spring-boot"
jar
{
baseName = "HOA"
version = "0.0.1-SNAPSHOT"
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories
{
mavenCentral()
maven { url "https://maven.atlassian.com/3rdparty/" }
maven { url "https://mvnrepository.com/artifact/" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies
{
compile group: "org.mindrot", name: "jbcrypt", version: "0.4-atlassian-1"
compile group: "org.eclipse.persistence", name: "javax.persistence", version: "2.1.1"
compile group: "org.springframework.data", name: "spring-data-jpa", version: "1.10.4.RELEASE"
compile group: "org.springframework.hateoas", name: "spring-hateoas", version: "0.21.0.RELEASE"
compile group: "com.h2database", name: "h2", version: "1.4.192"
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-groovy-templates")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-web")
// Automated Testing
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc")
}
dependencyManagement
{
imports
{
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR1"
}
}
compileJava
{
options.compilerArgs << "-Xlint:all" << "-Xdiags:verbose"
}
bootRepackage
{
mainClass = "com.latencyzero.hoa.Application"
}
bootRun
{
addResources = true
}
The messy one:
buildscript
{
ext
{
springBootVersion = '1.4.3.RELEASE'
}
repositories
{
mavenCentral()
}
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
archivesBaseName = 'toolcrib'
compileJava
{
options.compilerArgs << "-Xlint:all" << "-Xdiags:verbose"
}
jar
{
manifest
{
attributes 'Implementation-Title': 'ToolCrib',
'Implementation-Version': version
}
}
repositories
{
mavenCentral()
}
dependencyManagement
{
imports
{
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR3'
}
}
dependencies
{
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile('org.springframework.boot:spring-boot-devtools')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
runtime('org.postgresql:postgresql')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
springBoot
{
mainClass = "com.latencyzero.toolcrib.services.tools.ToolsServer"
}
bootRun
{
addResources = true
}
Thanks for any insight!
Have you had a look to the documentation
By default changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates will not trigger a restart but will trigger a live reload.
And
If you want to customize these exclusions you can use the spring.devtools.restart.exclude
There is also spring.devtools.restart.additional-exclude to add more excludes and keep the defaults. In your case, you should add the following to your configuration:
spring.devtools.restart.additional-exclude=classpath:/tools-server/templates/

Gradle Error: No method add() found for arguments

So I'm adding a custom plugin to Gradle, specifically jmeter. https://github.com/kulya/jmeter-gradle-plugin
I thought everything worked, but now this error is occuring:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\rstraubel\Documents\DataCentral\build.gradle' line: 21
* What went wrong:
A problem occurred evaluating root project 'DataCentral'.
> Could not find method add() for arguments [jmeterEditor, class com.github.kulya.gradle.plugins.jmeter.JmeterRunGuiTask] on task set.
Line 21 is just where I call plugin: 'jmeter'
Everything else specified in the github has been done. Any ideas? Thanks
Edit: Full Build.gradle
buildscript {
ext {
springBootVersion = '1.0.2.RELEASE'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone"}
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.github.kulya:jmeter-gradle-plugin:1.3.1-2.6")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'idea'
apply plugin: 'sonar-runner'
apply plugin: 'jmeter'
jar {
baseName = 'datacentral'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
maven { url "http://m2.neo4j.org" }
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
compile "org.springframework.boot:spring-boot-starter-data- mongodb:${springBootVersion}"
compile "com.fasterxml.jackson.core:jackson-databind"
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.data:spring-data-mongodb"
compile "org.mongodb:mongo-java-driver:2.12.0-rc0"
compile "org.springframework.data:spring-data-rest-webmvc"
testCompile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
}
jmeterRun.configure {
jmeterTestFiles = [file("src/test/jmeter/TestCases.jmx")]
}
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://hawkeye.control-tec.com:9000"
property "sonar.jdbc.url", "jdbc:mysql://bluestreak.qualifier.control-tec.com:3306/sonar?useUnicode=true&characterEncoding=utf8"
property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
property "sonar.jdbc.username", "sonar"
property "sonar.jdbc.password", "sonar-pass"
property "sonar.projectKey", "com.controltec.incontrol.qualifier:Data-Central"
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.0'
}
Most likely, the jmeter plugin hasn't been updated to support Gradle 2.0. You can verify by trying with 1.12.

Resources