Include react generated HTML/JS files from composite gradle build - java

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'
}
}

Related

How to deploy a Jar from a library project

I have a library project in Java which is several folders, each one doing specific parts and having its own dependencies.
Since I am working locally I would like to deploy this library locally and get the Jar to import to another project.
For this reason I am using gradle and what I did was going to the directory where I have all the folders of the library and gradle init and then gradle build.
Since I want the files locally, I saw that I can use gradle publishToMavenLocal, which I did and it created a jar file under ~/.m2/..... Now the issue is that this jar file appear to only contain a META-INF folder and inside of it a manifest.mf file.
This is the build.gradle file used.
What am I doing wrong? Should I do something different?
check gradle docs
there is also a complete example.
be sure to add your sourceSets that you want to compile and build in the jar.
build.gradle
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets {
main {
java { srcDir 'src/main/java' }
resources {
srcDirs 'src/main/resources'
}
}
test {
java { srcDir 'src/test/java' }
resources {
srcDirs 'src/test/resources'
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'project1-sample'
version = '1.1'
from components.java
}
}
}
You could also add your library project to your main project like this :
build.gradle
dependencies {
compile project(':library_project')
}
settings.gradle
rootProject.name = 'Project'
include ":library_project"
project(':library_project').projectDir = new File(settingsDir, '../library_project')

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?

Creating task in Gradle + Spring Boot to set default profile?

I just want to set the default profile when I run gradleRun, but this is failing with cannot find method run()
I'm first wondering:
What does the buildscript do for me and how can I successfully use the spring-boot plugin
Could not find method bootRun() for arguments [build_74d21ufxy8p9tyrqny7v4pkut$_run_closure1#389a9e15] on root project 'core' of type org.gradle.api.Project.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE"
}
}
task local {
run { systemProperty "spring.profiles.active", "development" }
}
bootRun.mustRunAfter local
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
apply plugin: 'spring-boot'
group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
Remove
task local {
run { systemProperty "spring.profiles.active", "development" }
}
bootRun.mustRunAfter local
Add
bootRun {
systemProperty "spring.profiles.active", "development"
}
To answer your questions about build script and spring-boot plugin, build script contains the tasks needed to build a project using gradle, this is simplistic description, check the documentation. Spring boot plugin
allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies
You can refere to the documentation here

Include distribution folder of frontend build in the JAR with Gradle

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"
}
}

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