How to exclude file from dependent jar when running sprint boot app? - java

I want to exclude a specific file from the base jar while running my project (which will use the base jar as dependency)
Base project structure looks like:-
baselib
|
|----src
| |
| |--some packages
| |
| |--resources
| | -- somefile.xml
|
|---target
Another project called "mycustomproject" will have "baselib" as dependency jar in pom.xml
My question i,s how to debug/run (Debug As/Run As-> Spring Boot App) the "mycustomproject" by excluding "somefile.xml" alone. I cannot remove this file in "baselib" since some one is using it.
Could someone share some inputs here?

Related

Build war files for services when modules are modified

I have a monorepo with the following structure:
app
|lib
| -moduleA
| |-pom.xml
| -moduleB
| |-pom.xml
|services
| |-foo
| |-pom.xml
| |bar
| |-pom.xml
| |gamma
| |-pom.xml
My end goal here is to deploy the application's war files to aws via GitHub actions. I am able to deploy individual services whenever there are changes in them, with the command mvn clean install -Dmaven.clean.failOnError=false -f services/foo/pom.xml.
However, I am not sure how to update the services whenever a module it depends on is modified. For eg., foo and gamma depend on moduleA. Whenever moduleA is modified, I would like to build war files only for foo and gamma and exclude bar since there are no changes for it. Pretty new to java, any ideas how to achieve this? TIA
Was able to find a flag that updates the dependencies of the module
--also-make-dependents

How to handle Whitelabel 404 error in Spring Boot without using Thymeleaf or other template engines

I have a Spring Boot + Spring Security application with React JS for client. I want to have a custom styled 404 error page in it without using Thymeleaf.
My Spring Boot version is 2.0.0.M2
The war file structure is
root-dir
|- WEB-INF
| |- lib
| |- classes
| | |- static
| | | |- * (react dist)
| | |- * (other files)
|- org
|- META-INF
My war file build process will be as follows.
Using Gradle npm plugin, build the ReactJS distribution
Gradle task that will copy the distribution to static directory in war
Build war finally
Please help me on this to solve.
No need to disable the white label page.
Just add a file named 404.html under the src/main/resources/public/error directory.
Baeldung.com indicates src/main/resources/public/errors, which does not work, at least with SpringBoot 2.1.x. Use the singular error and you will be good.
All of this work without any template engine in the project's dependencies.

how to set classpath for spring project in jshell

I'm learning to use spring framework and I can't seem to find a tool similar to Django shell_plus. I'd like to be in a java repl in which I have access to all my classes, dependencies and etc. I've googled and found j-shell and spring-shell but spring-shell doesn't seem to satisfy my needs since I need to be able to instantiate and play around with my classes, so I went with j-shell. my only problem is I don't know how to let j-shell know where my classes are. I've tried
$ jshell --classpath "~/Desktop/path/to/project/"
my project directory being:
Project
|
+-- build.gradle
|
+-- build
| |
| +-- libs and etc...
+-- src
| |
| +-- main
| |
| +-- java
| |
| com
| |
| +-- MyClass.java
| |
| +-- MyOtherClass.java
|
+-- settings.gradle
+-- gradlew
+-- default.nix
but when in j-shell I don't have access to spring framework or my classes. I'm using emacs on nixos if that matters and nix for dev environment dependencies. appreciate any kind of help, thanks.
i've also tried setting the classpath to the jars located in ~/home/.gradle/
I expect to be in jshell repl with access to springframework classes and ones i've coded myself.
There are a few ways you could set the class path for a jshell session.
On startup, before you enter the REPL:
$jshell --class-path <your_classpath>
After you're already in a running REPL session:
jshell>/env -class-path <your_classpath>
Notice the difference between --class-path and -class-path? That's not a typo.
The old-fashioned way. Set a $CLASSPATH environment variable before you start jshell:
$export CLASSPATH=<your_classpath>
$jshell
There is actually a fourth way which makes the class path persistent between jshell sessions so you don't have to set it every time. It involves creating a .jsh startup file, and setting and saving it. From within a jshell session, see /help to find out more.
I made a Gradle plugin to run a jshell console with all the libraries of your project in the classpath: jshell-plugin , once configured in your build setup, you only need to run:
$ gradle --console plain jshell
Also the README instructions have a special section to setup it in a Spring Boot project and be able to access to the business objects (the beans in the Spring context: #spring-boot-applications:
jshell> var myUserService = ctx.App.getBean(MyUserService.class)
jshell> ctx.App.ppjson(myUserService.getByUsername("admin"))
{
"name" : "Jhon",
"lastName" : "Due",
"username" : "admin",
"age" : null
}
If you are not using Gradle and you have an old Spring Boot 1.x project, you can use another project I made that does not work with modern Spring Boot applications, but at least you don't need Gradle or the jshell from a JDK9+ distrubution to play with your code and the spring's beans: spring-ctx-groovy

Load process definitions from multiple modules in camunda

I'm trying to configure Camunda automatic resource deployment for a multimodule project. What I want to archive is that for the following project structure:
\---learnCamunda
| pom.xml
+---application
| | pom.xml
| +---src
| \---main
| +---java
| | \---com
| | \---camunda
| | \---learnCamunda
| | | LearnCamundaApplication.java
| | \---configuration
| | CustomOracleDialect.java
| | FlywayConfiguration.java
| | H2ConsoleConfiguration.java
| \---resources
| | application.properties
| |
| \---processes
| loanRequest.bpmn
+---contract-management
| pom.xml
\---src
\---main
+---java
| \---com
| \---camunda
| \---learnCamunda
| +---dto
| +---dts
| +---entity
| \---enums
\---resources
\---processes
DUMMY_PROCESS.bpmn
where application and contract management are modules of learnCamunda project, I want my application.properties to provide classpath for Camunda resource scan to pickup both loanRequest.bpmn and DUMMY_PROCESS.bpmn. Is that possible via application.properties in application module? (The application module has dependency on contract-management module)
If it is possible then which camunda property -- should I set it and to what?
I have already tried different combinations of deployment-resource-pattern but non of them worked.
EDIT:
One thing i noticed is that PathMatchingResourcePatternResolver.findAllClassPathResources loads all resources from target, my compiled model is in target.classes.processes and classpath resource from resolver is target.classes so it seems it's not done recursively, but im having hard time to confirm this
You are correct to be looking at the deployment-resource-pattern property as the place to start. By default, it will include classpath*:**/*.bpmn so your file should be loaded.
That suggests the problem is either the value of deployment-resource-pattern has been changed or the contract-management jar is not on the classpath.
I suggest you try debugging the app to check the value of camunda.deployment-resource-pattern. Then have a look at the build process and make sure your application depends on contract-management, its the same version and the parent pom is building both of them.

How to generate directory as maven artifact

I'm currently working on changing building process of legacy project. Right now it is built by a custom-made solution and our aim is to adjust it to a standard one - maven.
This is a standalone application and current output of a build is a directory with following structure:
OUR_APPLICATION
|
|_bin
| |_start.sh
| |_stop.sh
|
|_etc
| |_app.properties
|
|_jar
| |_app_classes1.jar
| |_app_classes2.jar
|
|_lib
|_third_party_library.jar
I am wondering what's the best way to achieve similar output with maven and still follow best practises (or at least break them as little as possible).
I'm leaning towards creation of multi-module project (separate modules for Java code, configuration files and shell scripts) and then using maven-assembly-plugin for combining it all together, but I'm not entirely sure how (if at all) it can be properly done.
Still I'm not sure whether its the best fit for me and I will be very grateful for any feedback.
You are on the right way (imo): Maven is a good tool for producing deployable artifacts. And the maven-assembly-plugin fits the needs that you described. It could produce a ZIP file containing the application structure you described.
The default artifact type that Maven produces is simply a JAR. This is ok for libraries. The application structure that you described seems to have three of them: app_classe1.jar, app_classes2.jar, and third_party_library.jar (I know, there could be more of them).
The Maven setup that I would suggest (and keep in mind: other ways exist): Create a multi-module project, which has modules for each application JAR and one module for assembling them. The parent project then simply builds them all. Like this:
ParentProject
|
|-- pom.xml (the parent one, that describes all modules)
|
|-- Module1 (producing app_classes1.jar)
| |
| |-- pom.xml
| |-- src/...
|
|-- Module2 (producing app_classes2.jar)
| |
| |-- pom.xml
| |-- src/...
|
|-- AssemblyModule (the one that produces a ZIP, for example)
|
|-- pom.xml (with type POM because it does not produce a JAR)
The assembly module should not have the default artifact type (JAR), but should be set to type POM. The pom.xml of that assembly module then configures the maven-assembly-plugin to create a ZIP file. The content of this ZIP file is highly configurable. Additionally, this plugin attaches the result artifact (the ZIP) to the build, so that it will be uploaded to a repository, if that is also configured.

Categories

Resources