Comments (8)
Hi @tschaffter
Sorry, I didn't get to you sooner...
Indeed, the packages will always be installed in the default location (e.g. ~/.m2/repository
).
this is what Maven does by default, and there is no need to change it for the Nx caching to work.
(the cache not working right now, is another issue that you spotted in #111, I will fix that)
As for your approach (task dependency) it is totally valid, in fact, I plan to integrate that out-of-the-box in the plugin, see #66
So stay tuned!
from nxrocks.
Thanks for pointing out the executor @nxrocks/nx-spring-boot:buildImage
. I can confirm that using it to build an image of a Spring app that depends on a local library installed in ~/.m2/repositories
works (as described in this comment).
from nxrocks.
Hi @while1618,
Sorry for the late answer.
The idea of library
projects was to allow reusing them in your boot applications
(by adding the dependency in applications
build.gradle
or pom.xml
).
They are not meant to be executable like boot jars (I recently enhanced that with #75, based on this guide on spring.io). Just like they don't need to be embedded inside a parent project (multi-module) per se.
Regarding the second point, Spring Initializr doesn't support multi-module projects out-of-the-box (spring-io/initializr#663), so doesn't this plugin.
But with the support for Nx's dependency graph api, this is not a big deal, as one could have library projects built first, when running a command on the application that uses it (#66, work in progress)
from nxrocks.
Hi @tinesoft ,
Thanks to your comment, I was able to create a "@nxrocks/nx-spring-boot" library and import it in an app. I updated the build
target of the lib with the additional argument install
.
"build": {
"executor": "@nxrocks/nx-spring-boot:build",
"options": {
"root": "libs/my-lib",
"args": [
"install"
]
}
},
Currently the library is installed in the directory ~/.m2/repository
. One improvement I'm seeking to make is to install the library to a folder inside the Nx workspace so that Nx can cache the build artifact. Any idea on what the best way to achieve this would be? Thanks!
from nxrocks.
After giving it more thought, it's preferable to keep installing Maven packages to the default location (e.g. ~/.m2/repository
) to keep the environment as standard as possible. This means that the "install" operation should not be part of a nx-cacheable task like "build" because Nx is not caching ~/.m2/repository
.
Here is the solution I came up with:
- Add an "install" task to the library project.
"install": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"command": "./mvnw install",
"cwd": "libs/shared/data-access-java"
},
"dependsOn": [
{
"target": "build",
"projects": "self"
}
]
},
- In the app that imports the library, specify that the task "build" depends on the task "install" of dependencies.
"build": {
"executor": "@nxrocks/nx-spring-boot:build",
"options": {
"root": "apps/challenge-core-service",
"args": []
},
"dependsOn": [
{
"target": "install",
"projects": "dependencies"
}
]
},
@tinesoft What do you think of this approach? If it were to be kept, then adding to this plugin a task like @nxrocks/nx-spring-boot:install
that run ./mvnw install
under the hood would enable to make the library project.json
more compact than when using @nrwl/workspace:run-commands
as I do.
from nxrocks.
Hi @tinesoft
I want to propose a different strategy for installing a library so that it can be imported in an app. The motivation is to enable the app to be dockerized. Dockerizing the app when the library is installed to the local repository (~/.m2/repository
) is not possible in a simple way.
Instead, the strategy I'm exploring is to copy the jar library to the app folder when running the install
target of the library. Then, the idea is that the following Dockerfile could be used to dockerize the Spring Boot app. See the line COPY lib /workspace/lib
where I copy the lib folder that includes the jar file of the library.
FROM maven:3.8.4-openjdk-17 AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
COPY lib /workspace/lib
RUN mvn -f pom.xml clean package -DskipTests --no-transfer-progress
FROM openjdk:17-alpine
COPY --from=build /workspace/target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
I have created a GithHub repository (tschaffter/nx-spring-boot-example) that we could share to illustrate one recommended way to organize apps and libs generated with @nxrocks/nx-spring-boot
.
I currently have an issue where I cannot build the app because it can not successfully import the library. If you have the time, do you mind having a look at this broken feature branch to see what I'm doing wrong? My Java is a bit rusty so we can assume that I'm doing something dumb. 😉
from nxrocks.
I found the solution to the above issue, see #113.
from nxrocks.
I want to propose a different strategy for installing a library so that it can be imported in an app. The motivation is to enable the app to be dockerized. Dockerizing the app when the library is installed to the local repository (~/.m2/repository) is not possible in a simple way.
Instead, the strategy I'm exploring is to copy the jar library to the app folder when running the install target of the library. Then, the idea is that the following Dockerfile could be used to dockerize the Spring Boot app. See the line COPY lib /workspace/lib where I copy the lib folder that includes the jar file of the library.
FROM maven:3.8.4-openjdk-17 AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
COPY lib /workspace/lib
RUN mvn -f pom.xml clean package -DskipTests --no-transfer-progressFROM openjdk:17-alpine
COPY --from=build /workspace/target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
I have created a GithHub repository (tschaffter/nx-spring-boot-example) that we could share to illustrate one recommended way to organize apps and libs generated with @nxrocks/nx-spring-boot.
@tschaffter Note that Spring-boot has built-in support for generating an optimised Dockerfile
to package and run the application.
This is supported by the plugin as well, via the command nx buildImage bootapp
, so I would recommend to use that instead.
Your can customise some options of that command (like setting the docker Image name for e.g) by adding it in the args
of related target
in project.json
{
"version": 1,
"projects": {
"you-boot-app": {
"projectType": "application",
"root": "apps/your-boot-app",
"sourceRoot": "apps/your-boot-app/src",
"targets": {
"buildImage": {
"executor": "@nxrocks/nx-spring-boot:buildImage",
"options": {
"root": "apps/your-boot-app",
"args": ["-Dspring-boot.build-image.imageName=yourcompany/your-boot-app-docker"]
}
}
}
}},
"cli": {
"defaultCollection": "@nrwl/workspace"
}
}
from nxrocks.
Related Issues (20)
- [Bug] does not support schematics HOT 5
- [Bug] Nx unable to find fvm despite being present in path HOT 10
- Adopting Nx to SpringBoot MonoRepo's HOT 1
- Run arguments on project level are not pass to ./mvnw spring-boot:run only the ones that are coded to the project.json HOT 5
- [Feature] Configuration Variants HOT 1
- [Feature] (nx-spring-boot) Add Support for Java 21 HOT 2
- [chore] Reduce code smells reported by Sonarcloud
- [Feature] Make sure e2e + smoke tests pass on all OS
- Linking is failing with nx-spring-boot link command [Bug] HOT 1
- [bug] Spring packages are not cached HOT 1
- [Feature] Update plugins to support Nx 17 HOT 14
- [Bug] Support for nx v17.x.x HOT 6
- Executors not working in a multi-module project with more than one level of folders HOT 12
- [Feature] : support java 21 HOT 5
- [Question] Maven dependency management for monorepo libs HOT 16
- [Feature] Run custom Maven commands by nx-spring-boot HOT 2
- [Bug] create-nx-spring-boot fails to run HOT 3
- [Bug] createNodesFor is not a function since @nxrocks/nx-spring-boot 9.3.0 HOT 8
- Cached projects are only restored the "target" folder but not the ~/.m2/repository folder HOT 10
- [Bug] Cannot run any nx command after creating micronaut project HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nxrocks.