Can there be multiple war tasks in build.gradle to generate multiple wars with different dependencies - gradle

I am trying to do similar thing as: Create multiple .WAR files with different dependencies in Gradle
But the solution given in this post is not working. I am using gradle 2.5 to achieve this.
Expectation is to generate 3 different wars as output of the script. I created a simplest project with minimal dependencies. When i execute build.gradle, it just gives me single war named: Test-1.0.war. Test is name of the sample project i created.
Here is build.gradle:
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.5
version = '1.0'
task createStandardWar(type: War, dependsOn: classes) {
baseName = 'standard'
destinationDir = file("$buildDir/libs")
classifier = 'Functional'
}
task createStandardWarQasOnly(type: War, dependsOn: classes) {
baseName = 'standard-qas-only'
destinationDir = file("$buildDir/libs")
classifier = 'Functional2'
}
task createStandardWarQasAndLog4J(type: War, dependsOn: classes) {
baseName = 'standard-qas-log4j'
destinationDir = file("$buildDir/libs")
classifier = 'Functional3'
}
task createDists(dependsOn: [createStandardWar, createStandardWarQasOnly, createStandardWarQasAndLog4J])
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}

Related

How can I publish many dependant subprojects in a certain order with gradle?

I'm trying to publish two subprojects with gradle. I have two subprojects: SubprojectA is dependant on SubprojectB. I want to publish them both using maven-publish. I have this in build.gradle:
plugins {
id 'java'
id 'maven-publish'
}
publishing {
publications {
SubprojectA(MavenPublication) {
println(project(':subprojectA').tasks)
groupId group
artifactId 'subjprojectA'
version version
artifact project(':subprojectA').tasks.getByName('jar')
}
SubprojectB(MavenPublication) {
groupId group
artifactId 'subjprojectB'
version version
artifact project(':subjprojectB').tasks.getByName('jar')
}
}
repositories {
maven {
url "http://some-url"
}
}
}
Gradle tries to publish them in alphabetical order: Since subprojectA depends on subprojectB, it doesn't work because subprojectB hasn't been published yet. If I rename subprojectA to ZSubprojectA, like this:
ZSubprojectA(MavenPublication) {
println(project(':subprojectA').tasks)
groupId group
artifactId 'subprojectA'
version version
artifact project(':subprojectA').tasks.getByName('jar')
}
then it works, but I don't like this solution. I've tried something like this:
project(':subprojectA') {
publish.dependsOn(":subprojectB:build")
}
but it doesn't work, and :subprojectB:publish doesn't exist.
I've been able to solve it. This was the problem:
Both subprojects had their respective build.gradle files. In subprojectA's build.gradle there was something like this:
plugins {
id 'java'
}
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation group: 'com.neovisionaries', name: 'nv-i18n', version: '1.22'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.1'
implementation group: 'com.myorg.something', name: 'subprojectB', version: '1.0.0-SNAPSHOT'
}
Therefore it was trying to get subprojectB from my organization private repository, and I should have done something like this:
compile project(":subprojectB")
I should have include both build.gradle files in my question. But the problem is solved.

Create a jar with external dependencies using Gradle

I have added following in my build.gradle file.
jar {
doFirst {
into('lib') {
from configurations.runtime
}
}}
When I run "gradle build" it creates a JAR file containing all the dependent JARs. But, then it puts that JAR into a ZIP file containing the dependent jars in lib folder again. This ZIP file structure looks like this:
-- 4.11-SNAPSHOT.jar
---- lib
----- lib/dependent1.jar
----- lib/dependent2.jar
-- lib
---- dependent1.jar
---- dependent2.jar
I don't want the ZIP to be generated. I only want the single JAR containing all the dependencies to get generated.
-- 4.11-SNAPSHOT.jar
---- lib
----- lib/dependent1.jar
----- lib/dependent2.jar
Complete build.gradle is below:
repositories {
maven {
url 'https://repository.cloudera.com/content/repositories/releases/'
}
maven {
url 'https://repository.cloudera.com/content/repositories/third-party/'
}
}
configurations.all {
// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'com.company.projectname', name: 'domain', version: pppVersion
compile group: 'com.company.projectname', name: 'model', version: modelVersion
compile group: 'com.company.projectname', name: 'pipeline-util', version: pppVersion
compile group: 'com.cloudera.crunch', name: 'crunch', version: '0.3.0-3-cdh-5.2.1'
compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.5.0-cdh5.3.3'
compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-common', version: '2.5.0-cdh5.3.3'
compile group: 'ch.hsr', name: 'geohash', version: '1.3.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
manifest {
attributes 'Main-Class': 'com.company.projectname.delta.DeltaClusterer'
}
classifier 'job'
doFirst {
into('lib') {
from configurations.runtime
}
}
}
There is a parent build.gradle as well. And it is as follows:
/**
* Dependencies for build script plugins. Project dependencies should
* be added further in this file.
*/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.netflix.nebula:gradle-ospackage-plugin:2.2.0'
classpath 'org.hidetake:gradle-ssh-plugin:1.0.4'
classpath 'be.xvrt:release-plugin:0.5.1'
}
}
task wrapper( type: Wrapper ) {
gradleVersion = '2.2'
}
// Release script for automated version management.
// Apply at root so it uses/updates the root gradle.properties file.
apply plugin: 'be.xvrt.release'
subprojects {
apply plugin: 'java' // Support for Java.
apply plugin: 'maven-publish' // Upload artifacts to Nexus.
apply plugin: 'be.xvrt.release' // Release script for automated version management.
apply plugin: 'org.hidetake.ssh' // Execute SSH commands.
apply plugin: 'rpm' // Builds RPM artifacts.
apply plugin: 'sonar-runner' // Creates Sonar reports.
apply plugin: 'project-report' // Creates a dependency report, very useful for finding dependency conflicts.
/**
* Global configuration.
*/
sourceCompatibility = 1.6
targetCompatibility = 1.6
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
/**
* Global dependency settings for all projects. Per project dependencies
* should be added to the build.gradle file in every project.
*/
// Common dependencies for all projects.
dependencies {
// Also see resolutionStrategy below when modifying semantic-TTOM-ddct version!
compile group: 'com.teleatlas.models', name: 'semantic-TTOM-ddct', version: '4.0.2.0'
compile group: 'com.google.guava', name: 'guava', version: '15.0'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
compile group: 'args4j', name: 'args4j', version: '2.0.29'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.8'
}
// The install artifact is only required for command line applications.
if ( !tasks.findByPath( 'war' ) ) {
task buildInstallZip( type: Zip ) {
from jar.outputs.files
// Copy all dependencies into lib.
into( 'lib' ) {
from configurations.runtime.allArtifacts.files
from configurations.runtime
}
}
}
/**
* Publish to Nexus.
*/
artifacts {
archives buildSourcesJar
archives buildJavadocJar
if ( !tasks.findByPath( 'war' ) ) {
archives buildInstallZip
}
}
publishing {
publications {
maven( MavenPublication ) {
if ( tasks.findByPath( 'war' ) ) {
from components.web
}
else {
from components.java
}
artifact buildSourcesJar {
classifier 'sources'
}
artifact buildJavadocJar {
classifier 'javadoc'
}
if ( !tasks.findByPath( 'war' ) ) {
artifact buildInstallZip {
classifier 'install'
}
}
}
}
}
}
/**
* Build RPM artifacts for command line applications.
*/
configure( [project( ':project1' ),
project( ':project2' ),
project( ':project3' )] ) {
task rpm( type: Rpm, dependsOn: 'jar' ) {
if ( version.contains( 'SNAPSHOT' ) ) {
logger.info( 'Building SNAPSHOT RPM.' )
version project.version.replace( '-SNAPSHOT', ".${System.currentTimeMillis()}" )
}
else {
logger.info( 'Building RELEASE RPM.' )
version project.version
release '1'
}
vendor 'company'
packageGroup 'company'
permissionGroup "Applications/${project.name}"
arch NOARCH
os LINUX
into '/usr/share/java/'
from( jar.outputs.files ) {
into project.name
}
from( configurations.runtime ) {
into "${project.name}/lib"
}
}
// When publishing artifacts, the rpm is also published (and created).
publish.dependsOn publishRpm
publishRpm.dependsOn rpm
}
What needs to be done for this?
I'm guessing the problem is the buildInstallZip task you have defined in the parent build.gradle file. It looks like that task is duplicating the functionality yo're now adding to the jar task in the subproject.
Did you write the parent build file, or was that provided by someone else? Maybe 2 people are both trying to solve the same problem in 2 different ways?
Anyway, to solve, either disable the buildInstallZip task in that subproject, or remove your modifications from the jar task and instead use the zip produced by buildInstallZip as you application distribution.

How to compile generated and non-generated classes in gradle when they are dependent?

How to compile generated and non-generated classes in gradle when non-generated classes(.java) are dependent on generated classes(.java)?
My generated classes are in $buildDir/generated-sources/thrift
My non generated classes are in src/main/java
The generated classes (.java files) are need to compile the non generated classes (.java files again) to finally put everything under $buildDir/classes/main (all .class files should be here)
Another way to say this would be my non generated classes in (src/main/java) need generated classes as a library dependency (.class files) to compile and run. So far I have the below build.gradle file but it doesn't quite work
plugins {
id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"
group 'com.peernova'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile files('/usr/local/lib/thrift-0.9.2.jar')
compile 'org.slf4j:slf4j-log4j12:1.7.12'
testCompile group: 'junit', name: 'junit', version: '4.11'
//compile files("$buildDir/classes/main") {
builtBy 'compileThrift'
}
}
def generatedDir = "$buildDir/generated-sources/thrift"
compileThrift {
thriftExecutable "/usr/local/bin/thrift"
//sourceDir "src/main/thrift"
sourceDir "$buildDir/../../../lib/service_api"
createGenFolder false
}
task thrift(type: Exec) {
commandLine '/usr/local/bin/thrift'
}
compileJava {
dependsOn 'compileThrift'
}
task(run_server, dependsOn: 'classes', type: JavaExec) {
if ( project.hasProperty("appArgs") ) {
args Eval.me(appArgs)
}
main = 'com.apple.project.servicehandlers.server'
classpath = sourceSets.test.runtimeClasspath
}
idea.module.sourceDirs += file("build/generated-sources/thrift")

Gradle Plugin Execution Order

I have written this gradle file
group 'com.abhi'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'org.flywaydb.flyway'
sourceCompatibility = 1.8
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.flywaydb:flyway-gradle-plugin:3.2.1'
classpath 'org.jooq:jooq-codegen:3.7.1'
classpath 'com.h2database:h2:1.4.177'
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jooq', name: 'jooq', version: '3.7.1'
compile group: 'org.jooq', name: 'jooq-meta', version: '3.7.1'
compile group: 'org.jooq', name: 'jooq-codegen', version: '3.7.1'
runtime group: 'com.h2database', name: 'h2', version: '1.4.177'
}
flyway {
url = 'jdbc:h2:file:target/foobar'
user = 'sa'
}
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
.configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.7.0.xsd') {
jdbc() {
driver('org.h2.Driver')
url('dbc:h2:file:target/foobar')
user('sa')
password('')
}
generator() {
database() {
}
generate() {
}
target() {
packageName('com.abhi.jooq.models')
directory('src/main/java')
}
}
}
// Run the code generator
// ----------------------
org.jooq.util.GenerationTool.generate(
javax.xml.bind.JAXB.unmarshal(new StringReader(writer.toString()), org.jooq.util.jaxb.Configuration.class)
)
when I say gradle compile it throws an exception
Could not load schemata
java.lang.NullPointerException
at org.jooq.impl.MetaImpl.meta(MetaImpl.java:120)
at org.jooq.impl.MetaImpl.getCatalogs(MetaImpl.java:143)
at org.jooq.impl.MetaImpl.getSchemas(MetaImpl.java:168)
at org.jooq.util.jdbc.JDBCDatabase.getSchemasFromMeta(JDBCDatabase.java:135)
at org.jooq.util.jdbc.JDBCDatabase.getSchemata0(JDBCDatabase.java:124)
at org.jooq.util.AbstractDatabase.getSchemata(AbstractDatabase.java:279)
I think the problem is that the code at the bottom of the script executes much before the "flyway" plugin.
Is there a way I can ensure that the code below is executed only after the flyway plugin has executed?
Yes, if you suppose, the problem is in execution order, then you can modify it, just like with any other tasks. Take a look at the documentation of the flyway plugin. According to it, this plugin adds some extra tasks to your build script, for thos one tasks are: flywayMigrate, flywayClean etc. You can make any of your tasks depending (with dependsOn option) on the tasks form this plugin and make them running just after the plugin's job is done.

Spring-Boot html in /templates don't render from command line

I have read several online posts about this issue, but for some reason none of those helped me solve this. I looked at the spring-boot examples and I don't think I am doing much different than those.
I am using Spring-boot 1.2.1, Thymeleaf, embedded tomcat without issue in IntelliJ. However, when I do a "gradle clean build", move the jar file, and an application.properties file to a deploy directory. I then start on the command with:
java -jar edm-0.1.0.jar
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/loginPage", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)
I am letting spring-boot configure my site for the most part. I am using the recommended directory structure:
I can hit the index page, and the REST calls seem to work just fine.
UPDATE based upon feedback:
I don't know if this is a broken jar issue, but when I extract it the structure and files appear correct. I can run "gradle bootRun" from the top of my project structure just fine. But if I deploy the jar file and run it, I get an error:
Task 'bootRun' not found in root project 'edm'
Here is my build.gradle file in case there might be an issue in it:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'maven'
idea {
project {
//if you want to set specific jdk and language level
jdkName = '1.8'
languageLevel = '1.8'
}
}
jacoco {
toolVersion = "0.7.0.201403182114"
}
project.ext {
springBootVersion = '1.2.1.RELEASE'
}
configurations {
querydslapt
}
buildscript {
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url "http://repo.spring.io/libs-milestone" }
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
}
}
jar {
baseName = 'edm'
version = '0.1.0'
}
dependencies {
querydslapt group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '2.8.0', classifier: 'apt-one-jar', transitive: false
compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
compile("org.springframework.security:spring-security-web:4.0.0.M1")
compile("org.springframework.security:spring-security-config:4.0.0.M1")
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE')
compile('com.android.tools.build:gradle:1.0.1')
compile("org.hibernate:hibernate-core:4.3.4.Final")
compile("org.hibernate:hibernate-entitymanager:4.3.4.Final")
compile("org.hibernate:hibernate-validator")
compile("org.apache.velocity:velocity:1.7")
compile('javax.mail:mail:1.4.1')
compile("org.springframework:spring-context-support")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.5.0")
compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
compile('org.codehaus.groovy:groovy-all:2.2.1')
compile('org.jadira.usertype:usertype.jodatime:2.0.1')
compile("postgresql:postgresql:9.1-901.jdbc4")
testCompile('org.spockframework:spock-core:1.0-groovy-2.0-SNAPSHOT') {
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile('org.spockframework:spock-spring:1.0-groovy-2.0-SNAPSHOT') {
exclude group: 'org.spockframework', module: 'spock-core'
exclude group: 'org.spockframework', module: 'spring-beans'
exclude group: 'org.spockframework', module: 'spring-test'
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
// for the anontation processor
querydslapt group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '2.8.0', classifier: 'apt-one-jar', transitive: false
// for compiling
compile("com.mysema.querydsl:querydsl-jpa:3.3.3")
compile("com.mysema.querydsl:querydsl-apt:3.3.3")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.0'
}
project.configurations {
providedRuntime
}
project.bootRepackage {
enabled = true
}
I also tried adding some thymeleaf configuration (even though I am not explicitly using Thymeleaf) to my application.properties file:
liquibase.changeLog=classpath:/db/changelog/db.changelog-master.xml
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true
So I guess my question is, why does running the "java -jar" not let me view the webpages?
It turns out that my REST controllers were proceeded by "/" so login page became "//loginPage" and that was causing the problem. I found the answer here but it took me a while to realize that was the issue. Hopefully this answer will help out someone in the future as it as a bear to figure out.

Resources