Include distribution folder of frontend build in the JAR with Gradle - java

I am a gradle beginner and I am struggling to include the frontend distribution build folder in the backend jar (I use Spring Boot and the frontend is an ionic app). In the backend.gradle, I configured the jar-Task that should include the frontend-build folder (called www) into build folder of the backend. The jar task runs through, but the desired artifacts are not present in the backend-build folder and therefore not in the final jar. Would be glad for any help.
project structure:
project
build.gradle
settings.gradle
backend
--> backend.gradle
frontend
--> frontend.gradle
settings.gradle
include 'backend'
include 'frontend'
rootProject.children.each {
it.buildFileName = it.name + '.gradle'
}
build.gradle
allprojects {
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'idea'
repositories {
mavenCentral()
}
}
frontend.gradle
plugins {
id "com.moowork.node" version "1.2.0"
}
task clean(dependsOn: 'npm_run_clean') {
}
task build(dependsOn: 'npm_run_build') {
}
backend.gradle
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group = 'ch.renewinkler'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
jar {
from('frontend/www') {
into('public')
}
}
processResources.dependsOn(':frontend:build')
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

You need to tell gradle that the jar task depends on the frontend's build task, otherwise it could run the jar file before the build task, and thus have nothing to include in the jar.
It's also a better idea to refer to projects by their name, instead of using absolute paths:
jar {
dependsOn(':frontend:build')
into('public') {
from "${project(':frontend').projectDir}/www"
}
}

Related

Gradle: create a single JAR with dependencies

I am migrating our product's build from Ant to Gradle, having started from a single project and got the following error:
> Could not resolve all files for configuration ':Shared:serverbase:compileClasspath'.
> Could not find guava:guava:23.3-jre.
Searched in the following locations:
- https://jcenter.bintray.com/guava/guava/23.3-jre/guava-23.3-jre.pom
- file:/F:/pros/X/java/guava/guava-23.3-jre.xml
Required by:
project :Shared:serverbase
Why it is looking for xml-files instead of jar?
Here are my files:
build.gradle in project's root directory:
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6' // gwt compiler plugin
}
}
allprojects {
apply from: rootProject.file('libraries.gradle')
repositories {
jcenter()
ivy {
url "file://${rootProject.projectDir}/ThirdParty/java/"
patternLayout {
artifact "[organization]/[module](-[revision])(.[ext])"
}
}
}
}
subprojects {
apply plugin: 'java-library'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
compileJava.options.debugOptions.debugLevel = "lines,vars,source"
compileJava.options.compilerArgs += ["-nowarn"]
compileJava.options.debug = true
}
build.gradle of this single project:
sourceSets.main.java.srcDirs = ['src']
dependencies {
implementation "guava:guava:${guavaVersion}"
implementation "slf4j:jul-to-slf4j:${slf4jVersion}"
implementation "logback:logback-classic:${logbackVersion}"
}
jar {
manifest {
attributes 'Class-Path': configurations.compileClasspath.collect { it.getName() }.join(' ')
}
}
settings.gradle:
rootProject.name = 'X'
include 'Shared:serverbase'
libraries.gradle:
ext {
...
guavaVersion = '23.3-jre'
...
}
(some content stripped)
And if I add file dependency to build.gradle as local file (How to add local .jar file dependency to build.gradle file?)
implementation files("guava-${guavaVersion}.jar")
I got tons of errors like 'error: package org.slf4j does not exist' so it seems that dependencies were not satisfied at all.
The outcome should be a single jar with compiled sources.
I am a novice in gradle, please forgive my unenlightenment.
Your Guava dependency is not correct.
Change from:
implementation "guava:guava:${guavaVersion}"
To:
implementation "com.google.guava:guava:${guavaVersion}"

How do I generate Maven POMs for multi-module builds with Gradle?

In order to not manually manage, both, Maven and Gradle build configuration files, I wanted to let Gradle generate the Maven POMs for a multi-module build.
This is working so far. Below you find the settings.gradle
rootProject.name = 'parent'
include 'module-a'
include 'module-b'
and here follows build.gradle
allprojects {
apply plugin: 'maven'
group = 'com.example.project'
version = '1.0-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
task createPom << {
pom {
project {
parent {
groupId project.group
artifactId rootProject.name
version project.version
}
}
}.writeTo("pom.xml")
}
}
task createPom << {
pom {
project {
packaging 'pom'
modules {
module 'module-a'
module 'modula-b'
}
}
}.writeTo("pom.xml")
}
The problem is that I have to manually declare the modules in the createPom task of the root project. Also, I need two dedicated tasks; one for the root project and and for the subprojects.
How can I let Gradle figure out what the modules are? Or is there a way to programmatically determine and add the subprojects as modules? Furthermore, is it even necessary to have two distinct tasks?

Include react generated HTML/JS files from composite gradle build

I have 2 Gradle projects that I want to link together. The first project has a standard Spring Boot app, and I want to link it with a ReactJS project that I've added a "build.gradle" file to.
I want to include the files generated into the "build/" directory of the ReactJS project into the "META-INF/resources" directory of the Spring Boot project.
These are my gradle.build files:
Spring Boot Gradle project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group = "com.myproject"
version = "1.0"
bootRun {
systemProperties = System.properties
}
repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.myproject:myproject-ui:1.0")
}
ReactJS Gradle project:
plugins {
id "com.moowork.node" version "1.1.1"
}
node {
download = true
}
apply plugin: 'java'
group = "com.myproject"
version = "1.0"
buildDir = 'dist'
task webjar(type: Jar, dependsOn: 'jar') {
from(fileTree('build')) {
into 'META-INF/resources'
}
}
build.dependsOn(webjar)
build.dependsOn(npm_run_build)
clean {
delete 'dist'
delete 'build'
}
Assume I've setup the "settings.gradle" files correctly such that it is a composite build, because that doesn't seem to cause issues.
When I run the "build" task for the ReactJS project, the "build/" directory is successfully generated, but I doesn't seem to be included by the Spring Boot project anywhere. How can I include the generated HTML/JS files such that they show up when I run the "bootRun" task of the Spring Boot project.
Typically you'd use a Configuration for this
reactjs/build.gradle
configurations {
wj
}
task webjar(type: Jar) { ... }
dependencies {
wj files(webjar)
}
springboot/build.gradle
configurations {
wj
}
dependencies {
wj project(path: ':reactjs', configuration: 'wj')
}
war {
with copySpec {
from zipTree(configurations.wj.singleFile)
into 'META-INF/resources'
}
}

build using gradle instead of eclipse

I'm very new to whole tomcat/spring/eclipse world.
I used gradle a little bit for android projects.
This is a project with tomcat/spring/eclipse and I 'd like to build it with gradle.
I copied a build.gradle file from one of tutorial on the web.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Now I run > gradle build and I see tons of errors which says 'org.springframework.*** does not exist'
I guess I need to somehow tell gradle to include *.jar files under
WebContent/WEB-INF/lib directory, but don't know how.
Please let me know if need to supply more info.
To add all jar files from WebContent/WEB-INF/lib and subfolders you must include the first line:
dependencies {
compile(fileTree(dir: "WebContent/WEB-INF/lib", include: "**/*.jar"))
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
testCompile("junit:junit")
}

Gradle eclipse using incorrect version for dependencies

I'm migrating a project to gradle to resolve dependencies, generate the eclipse project and build the project and I'm with a problem with the version of some dependencies on eclipse project. Here's the build.gradle of the project. It's an EAR with sub-projects.
apply plugin: 'ear'
def eclipseJbossName = 'org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/JBoss 6.x Runtime'
def defaultEarConfig = rootDir.getAbsolutePath() + '/ear.gradle'
allprojects {
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
eclipse {
classpath {
containers eclipseJbossName
}
}
repositories {
maven {
name = "mvnrepository"
url = "http://mvnrepository.com/"
}
mavenCentral()
maven {
name = 'jboss'
url = 'http://repository.jboss.org/nexus/content/groups/public/'
}
}
}
subprojects {
configurations {
provided
}
apply plugin: 'java'
sourceCompatibility = 1.6
targetCompatibility = 1.6
sourceSets {
main {
compileClasspath += configurations.provided
compileClasspath += configurations.compile
compileClasspath += configurations.runtime
}
}
}
dependencies {
deploy (project(path: 'anEJB')) {
transitive = false
}
deploy (project(path: 'anotherEJB')) {
transitive = false
}
deploy (project(path: 'aWAR', configuration: 'archives')) {
transitive = false
}
earlib ('commons-beanutils:commons-beanutils:1.6') {
transitive = false
}
//lots of other dependencies and all with transitive=false
}
//configuration of my subprojects
When I call gradle ear it builds and generate my artifact correctly. In the lib directory inside the root of the EAR there's all my earlibs with its correct versions. When I call gradle cleanEclipse eclipse it generates my project right, but when I see the build path inside eclipse it is using an incorrect version for commons beanutils. It is using the version 1.8. This is not happening for all my dependencies, but there are others with this problem. I've put all to not resolve the transitive dependencies.
You can click the project in Eclipse with right mouse button, then click properties. And select "Java Build Path" then click "Libraries" tab. Finally remove all jars besides "Gradle Dependencies" & "JRE System Libraries".
Done. Try it.

Categories

Resources