Build angular 11 with Spring boot 2.x in one single jar - java

I am facing an issue bulding an angular 11 with Springboot jar.
If I runn separatelly the apps (the backend on 8080 and the ui on 4200) they work just fine, the connection and calls in between are ok and stable.
What I am trying to do is actually integrate this. I've seen plenty of examples but I don't know where and what Am I doing wrong.
Here is a picture of the Project structure:
Here are the maven plugins which are installing and copying the ui build stuff into the jar.
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<nodeVersion>${node.version}</nodeVersion>
<workingDirectory>${frontendSrcDir}</workingDirectory>
<installDirectory>${project.build.directory}</installDirectory>
<environmentVariables>
<CI>true</CI>
</environmentVariables>
<!--suppress UnresolvedMavenProperty -->
<skip>${skip.ui}</skip>
</configuration>
<executions>
<execution>
<id>install-frontend-tools</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v14.16.0</nodeVersion>
<npmVersion>6.14.13</npmVersion>
</configuration>
</execution>
<execution>
<id>npm-install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>build-frontend</id>
<goals>
<goal>npm</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<arguments>run build --prod</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>angular-build</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>validate</phase>
<configuration>
<outputDirectory>${project.build.outputDirectory}/static</outputDirectory>
<resources>
<resource>
<directory>${frontendSrcDir}/dist/ui</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
In the jar the build files are copied under jar/BOOT-INF/classes/static.
Here is the image
What Am I missing?

Spring boot automatically serves static content if you have a folder in "resources" called "static" or "public". So put all of the compiled files from angular in a folder called "static" under resources.
Doing so will make spring boot serve angular :)

In #Aborgh answer,
So put all of the compiled files from angular in a folder called "static" under resources.
That is correct, and I am just adding some more. Specifically, when you run the build command such as npm build, it generates static files such as xxx.html, xxx.js, xxx.css in the dist folder.
So you want to copy all the files under dist and paste them into your Springboot project, place them right into the resources/static folder.
Not relevant to your question, but I would recommend you put the static files somewhere separated from your Springboot backend application. Try reverse proxies such as Nginx so you can resolve requests such as https://example.com/api/xxx to your Springboot backend application and all other requests such as https://example.com/foo/xxx to your frontend Angular views.

Related

why do I not find generated source files for grpc and protobuf when using maven and java in vscode

I have a pom file that correctly generates the grpc and protobuf source files I need in target/generated-sources when run from the command line. But when I build in vscode those directories are empty and references to the protobufs are undefined. Here's the section of my pom file that builds the grpc source.
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
I added the following based on what I read in other posts to prevent vscode/eclipse from removing the generated source directories
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/protobuf/grpc-java</source>
<source>/target/generated-sources/protobuf/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
When vs-code builds, the target generated-source directories are there, but there is no source in them. We had a similar problem with intellij but were able to work around it by running the maven command line build before opening intellij but that does not seem to work for vscode.
Use protoc-jar-maven-plugin instead.
Sample usage please view protoc-jar-maven-plugin.

Spring boot with maven and tanuki wrapper

I have spring boot app and i want to run it as service. I am using tanuki wrapper to do so :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.8.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate-daemons</goal>
</goals>
</execution>
</executions>
<configuration>
<logsDirectory>logs</logsDirectory>
<daemons>
<daemon>
<id>DataCopier</id>
<wrapperMainClass>org.tanukisoftware.wrapper.WrapperSimpleApp</wrapperMainClass>
<mainClass>com.myProject.mainClass</mainClass>
<platforms>
<platform>jsw</platform>
</platforms>
</daemon>
</daemons>
</configuration>
</plugin>
However how do i run this? I included it as plugin. I tried to mvn install it to see if this generates something but it does not generate anything in target folder. How do i run this then?

spring-boot-maven-plugin doesn't create fat jar

I'm using spring-boot-maven-plugin to package my REST service. I'm building the jar using mvn clean install or mvn clean package. After I decompile the jar, I don't find any of the dependencies added (I was expecting it to be a fat jar with all dependencies)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.9.RELEASE</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>true</executable>
<finalName>myapp</finalName>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
When I run the spring boot using java -jar myapp.jar -Drun.jvmArguments="-Dspring.profiles.active=qal" I'm getting ClassNotFoundException for many of the classes. It's clear that artifact didn't build as expected. However, if I start spring boot application using maven ./mvnw spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=qal" I guess, it finds all the dependencies in target folder hence works fine. How can I fix the build issue so that I can start app using java -jar command.
EDIT: It's multi-module maven project
it seems you are using a wrong command. mvn clean package is maven command, you should use command 'repackage', it used for
Repackages existing JAR and WAR archives so that they can be executed
from the command line using java -jar
as it mentioned here https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
Or probably it's plugin configuration issue. Just checked: it works with spring-boot-maven-plugin-2.0.0.RELEASE
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
Use this one
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
<executable>true</executable>
<fork>true</fork>
<!-- Enable the line below to have remote debugging of your application on port 5005
<jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
-->
</configuration>
</plugin>

With maven, how to modify deploy files just prior to deploy phase?

I have a maven project with one war and several ear projects. Each ear project requires a slightly different war/WEB-INF/web.xml. Each ear's pom.xml uses com.google.code.maven-replacer-plugin:replacer and org.codehaus.mojo:truezip-maven-plugin to replace tokens in the web.xml, and then place that new web.xml in the final <project>-app.ear/web.war/WEB-INF. This all works great with building and creating the final EAR artifacts.
The problem I'm having is that when I run (using Netbeans, but that shouldn't matter), the web.xml used for deployment (<project>/target/gfdeploy/first-app/web_war/WEB-INF/web.xml) is the tokenized version. I tried adding execution phases for deploy, but that doesn't work.
How can I ensure that the run deploy has the modified web.xml so I can test my app during development?
Here is the relevant parts of the ear pom.xml:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>package-replace</id>
<phase>package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
<execution>
<id>deploy-replace</id>
<phase>deploy</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.parent.basedir}/${web.xml}</file>
<outputFile>${project.build.directory}/${web.xml}</outputFile>
<replacements>
<replacement>
<token>#REALM_NAME#</token>
<value>${web.realm}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>package-replace-web-xml</id>
<goals>
<goal>copy</goal>
</goals>
<phase>package</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${web.xml}</source>
<outputDirectory>${project.build.directory}/${project.build.finalName}/${web.zip}/WEB-INF</outputDirectory>
</file>
</files>
</configuration>
</execution>
<execution>
<id>package-replace-web</id>
<goals>
<goal>copy</goal>
</goals>
<phase>package</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}/${web.zip}</source>
<outputDirectory>${project.build.directory}/${project.build.finalName}.ear</outputDirectory>
</file>
</files>
</configuration>
</execution>
<execution>
<id>deploy-replace-web-xml</id>
<goals>
<goal>copy</goal>
</goals>
<phase>deploy</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${web.xml}</source>
<outputDirectory>${project.build.directory}/gfdeploy/${project.artifactId}/web-${project.version}_war/WEB-INF</outputDirectory>
</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
I suggest you to keep your default src/main/webapp/WEB-INF/web.xml fully functional for running during development. Then, keep a similar file called src/main/webapp/WEB-INF/web-ear.xml with all the replacement preparation.
Wrap all your replacement plugin strategy inside a maven profile and targeted to the web-ear.xml file. Add to this profile a maven-war-plugin configuration that will use the alternative web-ear.xml file during build, instead of the default web.xml (check: http://maven.apache.org/plugins/maven-war-plugin/):
<profiles>
<profile>
<id>change-war-profile</id>
<build>
<plugins>
<!-- all your replacement plugins here -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-ear.xml</webXml>
</configuration>
</plugin>
<plugins>
</build>
</profile>
</profiles>
Make sure to activate this profile during the EAR maven build:
mvn package -Pchange-war-profile
you can run your war with the jetty-maven-plugin choosing the run-war goal.
That goal run the packaged war.
See: https://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html#running-assembled-webapp-as-war
First of all, deploy phase (of the build lifecycle) means deployment a production ready artifact to the remote Maven repository (e.g., Nexus or Artifactory). Roughly speaking, artifact deploy can be treated as copying the artifact. Read the Introduction to the Build Lifecycle for more details.
Secondly, Apache Maven does not support application deploy to the application server out-of-the-box. However, there are several ways to do it, depending on the application server you use. Which one do you use?
Special plugin can be found for JBoss Application Server - jboss-as-maven-plugin. See usage example:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.9.Final</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
...
</project>
Similar plugin can be found for GlassFish Server: glassfish-maven-plugin.
Also, if this is acceptable for you, you can perform 2-steps deploy from Netbeans:
Build the project: run mvn package with all your plugins are configured at package phase.
Deploy the project: run application on the app server from Netbeans if it is supported (See NetBeans Java EE Servers Support page).
Hope this helps.

How to automaticly copy a files from SVN repository

I have a project named HelloWorld. It is a maven java project.
I have got a repository like https://my.repo.com/svn/filesToCopy
What i want to do is:
Automaticly coping 3 files (a.xsd, b.xsd, c.xsd) from SVN repository into my src/main/resource directory.
I was trying to add smth like that in my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Coping one file</echo>
<copy file="https://my.repo.com/svn/filesToCopy/a.xsd" todir="${basedir}/src/main/resource" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
And then just building a project but it isnt working at all.
What is more im wondering if passing a https link need a username and password somewhere.
Any ideas?
Have you tried this approach? It's from Maven SCM plugin
<execution>
<id>perform-checkout</id>
<configuration>
<connectionUrl>myUrl</connectionUrl>
<checkoutDirectory>myDirectory</checkoutDirectory>
<excludes>folder1</excludes>
</configuration>
<phase><!-- some phase --></phase>
<goals>
<goal>checkout</goal>
</goals>
</execution>

Categories