Cannot load static file in tomcat - java

Firstly, I build the vuejs project in springboot by adding the following plugin:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<workingDirectory>src/main/frontend</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and yarn</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
<configuration>
<nodeVersion>${nodejs.version}</nodeVersion>
<yarnVersion>v1.22.4</yarnVersion>
</configuration>
</execution>
<execution>
<id>yarn install</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>yarn build</id>
<goals>
<goal>yarn</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-frontend</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes/resources/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>src/main/frontend/dist</directory>
<includes>
<include>static/</include>
<include>index.html</include>
<include>favicon.ico</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I have successfully built and run it when building into a jar file.
But now I want to run it with tomcat so I change the build format to war file but when running it can't load static file.
I think it's due to context path:
When run in tomcat: http://localhost:8080/report_system-1.0/
When run jar file: http://localhost:8080
But I don't know how to fix that.

You need to set publicPath when building the app for different environments. Something like
module.exports = {
publicPath: process.env.NODE_ENV === 'production'
? '/report_system-1.0/'
: '/'
}

Related

How to make Tomcat run a Spring/React app (Showing 404 not found)?

I have created an app with Spring Boot and React. It works perfectly fine if I run via Run As -> Spring Boot App. But when I make a war package with Maven (Clean Package) it shows 404 not found when I deploy it via Tomcat manager. My build plugins:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<nodeVersion>${node.version}</nodeVersion>
<npmVersion>${npm.version}</npmVersion>
<workingDirectory>${frontend-src-dir}</workingDirectory>
<installDirectory>${project.build.directory}</installDirectory>
</configuration>
<executions>
<execution>
<id>install-frontend-tools</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<nodeVersion>${node.version}</nodeVersion>
<npmVersion>${npm.version}</npmVersion>
</configuration>
</execution>
<execution>
<id>npm-install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>build-frontend</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>position-react-build</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<outputDirectory>${project.build.outputDirectory}/static</outputDirectory>
<resources>
<resource>
<directory>${frontend-src-dir}/build</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I have a guess it has something to do with port 3000 in React (though I proxied in json package). My frontend is under src/main/frontend. in main I use
#SpringBootApplication
public class MapRunnerApp extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MapRunnerApp.class);
}
public static void main(String[] args) {
SpringApplication.run(MapRunnerApp.class, args);
}
}

Maven <executions> and npm build

I have a hybrid application : Spring Boot and ReactJS.
While running/building/generating WAR, I have set the pom.xml to copy the npm run build generated files from dist folder to template(spring) folder.
But while running mvn clean install, file are copied before the npm run build command and then the npm run build and WAR is generated. That'swhy the frontend files are stale.
My pom.xml example is :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>prepare-package</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/public</outputDirectory>
<outputDirectory>${basedir}/src/main/resources/templates</outputDirectory>
<resources>
<resource>
<directory>${basedir}/dist</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>prepare-package2</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/resources/static</outputDirectory>
<resources>
<resource>
<directory>${basedir}/dist/static</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v11.10.0</nodeVersion>
<npmVersion>6.7.0</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
<!-- <execution>
<id>test</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>test</phase>
<configuration>
<arguments>test</arguments>
<environmentVariables>
<CI>true</CI>
</environmentVariables>
</configuration>
</execution> -->
</executions>
</plugin>
</plugins>
</build>
How do I configure so that the files are copied after npm run build ?
For similar task it makes sence to use gradle instead of maven. Tasks are much more flexible.
For example:
task buildFE(type: Exec) {
workingDir '../../FrontEnd'
commandLine 'buildFE.bat'
}
task CopyJava {
copy{
from '../OpenJDK'
into '../../Release/OpenJDK'
}
}

How to convert snippets to asciidoc html in eclipse

how to convert generated snippets to ASCII-doc html in spring boot.
i have already tried added the ASCII plugin.
i have already created .adoc file in src/main/asciidoc, but after maven build, it does not generate the html
include::{snippets}/test_find_by_id/http-request.adoc[]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Documentation.java</include>
</includes>
<systemPropertyVariables>
<org.springframework.restdocs.outputDir>
${snippetsDirectory}
</org.springframework.restdocs.outputDir>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>html</backend>
<doctype>book</doctype>
<attributes>
<snippets>${snippetsDirectory}</snippets>
</attributes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<!-- <phase>process-resources</phase>-->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.outputDirectory}/static/docs
</outputDirectory>
<resources>
<resource>
<directory>
${project.build.directory}/generated-docs
</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
You need to reference the ASCII Doc Maven plugin in your pom.xml. See: https://github.com/asciidoctor/asciidoctor-maven-plugin#installation
mvn prepare-package
is the solution

How to execute a plugin explicitly after a certain phase in maven?

I'm using javafx-maven plugin to create a javafx webstart application. I had some issues signing the jar files with the javafx-maven plugin. what I want to do is, package(jar) the application with javafx-maven plugin and then sign the jar files using maven-jarsigner-plugin .
How do i execute the maven-jarsigner-plugin to sign my files after the application is packaged?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<archiveDirectory>target/jfx/app/</archiveDirectory>
<includes>
<include>**/*.jar</include>
</includes>
<keystore>path tp keystore</keystore>
<alias>alias</alias>
<storepass>password</storepass>
<keypass>password</keypass>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.3.0</version>
<configuration>
<bundler>jnlp</bundler>
<mainClass>com.myorg.myapp.launcher.myappLauncher</mainClass>
<bundleArguments>
<jnlp.allPermisions>true</jnlp.allPermisions>
<jnlp.includeDT>true</jnlp.includeDT>
<jnlp.outfile>myapp</jnlp.outfile>
</bundleArguments>
</configuration>
</plugin>
To workaround this I moved signing to verify phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>signing</id>
<goals>
<goal>sign</goal>
<goal>verify</goal>
</goals>
<phase>verify</phase>
<inherited>true</inherited>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
Then I invoke maven like this:
mvn verify
Or make verify your default goal
Alternatively you can move javafx-maven plugin to the "prepare-package" phase:
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.0</version>
<configuration>
<bundler>jnlp</bundler>
<mainClass>com.myorg.myapp.launcher.myappLauncher</mainClass>
<bundleArguments>
<jnlp.allPermisions>true</jnlp.allPermisions>
<jnlp.includeDT>true</jnlp.includeDT>
<jnlp.outfile>myapp</jnlp.outfile>
</bundleArguments>
</configuration>
<executions>
<execution>
<id>create-jfxjar</id>
<phase>prepare-package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>

Maven wildfly-maven-plugin custom standalone.xml

How can I use a custom standalone.xml when running wildfly-maven-plugin:start (not just adding datasources, drivers,...)?
Using wildfly-maven-plugin-1.0.2.Final:
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.2.Final</version>
...
<execution>
<id>pre-integration-phase</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
<goal>deploy</goal>
</goals>
</execution>
...
I have tried using maven-resources-plugin for copying resources (standalone.xml) in wildfly configuration folder, but it seems that wildfly:start overwrites and deletes every (other) file in Wildfly (configuration) directory.
This task is used for jenkins integration testing, thats why I need jboss running just when testing is in progress.
You can unpack the WildFly installation and then set jbossHome configuration element in wildfly-maven-plugin.
Example configuration:
<version.wildfly>8.2.0.Final</version.wildfly>
<jboss.home>${project.build.directory}/wildfly-${version.wildfly}</jboss.home>
<server.config>standalone.xml</server.config>
<!-- Unpack the distribution -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-wildfly</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<inherited>false</inherited>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-dist</artifactId>
<version>${version.wildfly}</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- Copy server configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${version.plugin.resources}</version>
<executions>
<execution>
<id>copy-standalone-xml</id>
<phase>generate-test-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${jboss.home}/standalone/configuration</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- WildFly plugin to deploy war -->
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
<configuration>
<jbossHome>${jboss.home}</jbossHome>
<serverConfig>${server.config}</serverConfig>
</configuration>
<executions>
<execution>
<id>pre-integration-phase</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>

Categories

Resources