How to exclude resource folder during fat jar generation in spring boot by using maven build? - spring-boot

In my spring boot project, during the build process, I'm generating the fat jar and boot jar for my application.
Usecase: I have a scenario where I need to exclude the resource folder from the fat jar at the same time I need to include the same resource folder into my boot jar.
Below snippet, I have used in my project
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>boot</classifier>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Using the above snippet, resource folders are getting excluded in both fat and boot jars. can someone please guide me to achieve this use-case?
Java version: 1.8
Maveen version: 3.6.3
Spring Boot version: v2.1.2.RELEASE

Related

how to include static resource from a dependency in spring boot fat jar at the time of build

I have a spring boot application whose UI separately is built and added as a dependency to my application. Now I want to unpack the dependency and add to the resource folder of the spring boot application at the time of build so that it becomes a part of fat jar. Could someone guide me as to how this can be done with spring-boot-maven-plugin.
note: the project is using maven for build
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>nflow-explorer</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>io.nflow</includeGroupIds>
<includeArtifactIds>nflow-explorer</includeArtifactIds>
<outputDirectory>
${project.build.directory}/resources/main/static/explorer
<!-- or: ${project.basedir}/wherever/you/want/it -->
</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You don't have to unpack the JAR.
Simply use the Maven Resource Plugin http://maven.apache.org/plugins/maven-resources-plugin/index.html
You can specify directories to include like this:
<project>
...
<build>
...
<resources>
<resource>
<directory>[your folder here]</directory>
</resource>
</resources>
...
</build>
...
</project>

Use artifact version number in OpenAPI spec definition

I have a maven project with my API definition using the OpenaAPI v3 specification.
I generate the code using the openapi-generator-maven-plugin and all works fine. I'm also able to access the swagger-ui and view and test my API.
The problem is that I don't want to maintain the version number twice. So I'd like to refer the version number from my maven pom in my api spec instead of duplicating it with the risk of it getting outdated.
I've tried maven resource filtering, which seems to work. Since the yaml file in the target folder gets it nicely replaced when I put ${project.version} in the version field in the spec, but when I open the swagger-ui it prints "${project.version}" literally instead of the actual version.
This is the plugin config from my pom:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.build.resources[0].directory}/spec.yml</inputSpec>
<ignoreFileOverride>${project.build.resources[0].directory}/.openapi-codegen-ignore</ignoreFileOverride>
<language>spring</language>
<library>spring-boot</library>
<configOptions>
<!-- Use the newer java.time package instead of outdated java.util-->
<dateLibrary>java8</dateLibrary>
</configOptions>
<apiPackage>${default.package}.api</apiPackage>
<modelPackage>${default.package}.model</modelPackage>
<invokerPackage>${default.package}.invoker</invokerPackage>
<generateApiTests>false</generateApiTests>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
This is my spec.yml:
openapi: 3.0.0
info:
version: ${project.version}
...
Thanks to the comment from #bcoughlan I was able to get the maven-resources-plugin working as desired.
Now it takes filters the *yml file from the src/main/resources folder and replaces the ${project.version} placeholder during the generates-sources phase. Because the openapi-generator-maven-plugin executes in the same phase and is defined later, it will be executed after the resource filtering and will operate on the api spec in the target folder as a result of ${project.build.outputDirectory}/spec.yml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Codegen plugin for api. Depends on maven-resources-plugin to have executed in the generate-sources phase! -->
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.build.outputDirectory}/spec.yml</inputSpec>
<ignoreFileOverride>${project.basedir}/.openapi-codegen-ignore</ignoreFileOverride>
<language>spring</language>
<library>spring-boot</library>
<configOptions>
<!-- Use the newer java.time package instead of outdated java.util-->
<dateLibrary>java8</dateLibrary>
</configOptions>
<apiPackage>${default.package}.api</apiPackage>
<modelPackage>${default.package}.model</modelPackage>
<invokerPackage>${default.package}.invoker</invokerPackage>
<generateApiTests>false</generateApiTests>
</configuration>
</execution>
</executions>
</plugin>
Note: the openapi-generator-maven-plugin is almost the same as the swagger-codegen-maven-plugin. More about the difference can be found here: https://openapi-generator.tech/docs/fork-qna
Note 2: When using spring-boot parent pom, you inherit resource.delimeter=# property and ${project.version} notation no longer works. So replacing the placeholder with #project.version# should fix that.

maven-ejb-plugin: include generated sources

I have an EJB-maven-Project that has some generated classes (generated by JAXB).
They are generated into: target/generated-sources/jaxb/
Now, with maven-ejb-plugin I want them (i.e. their compilated classes) to be included into the client-jar, something like that:
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- Tell Maven we are using EJB 3.1 -->
<ejbVersion>3.1</ejbVersion>
<generateClient>true</generateClient>
<clientIncludes>
<clientInclude>com/bla/ch/authorization/client/**</clientInclude>
<clientInclude>target/generated-sources/jaxb/**</clientInclude>
</clientIncludes>
</configuration>
</plugin>
This does not work, the generated classes are not part of the ejb-client-jar. (Though they are in the ejb-jar).
How can I do this correctly?
Include sources in your jar is probably not a good solution.
You may add generated sources in you resources locations, and then use source-plugin, to generate the so called artifact-sources.jar
<resources>
<resource>
<directory>${basedir}/target/generated-sources</directory>
<filtering>true</filtering>
</resource>
</resources>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is a better way than producing a jar with source code.

How can I generate Flash HTML wrappers in a WAR using Flexmojos and Maven 3?

I have two Maven projects: one to build a SWF and another one to build a WAR that contains it. The WAR needs to contain a Flash wrapper for the SWF. Flexmojos is apparently capable of doing this via the HTML wrapper mojo, but this doesn't work with Maven 3. See here for more details.
Is there a workaround for this?
My SWF POM has the following:
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>4.1-beta</version>
<configuration>
<parameters>
<swf>${project.artifactId}-${project.version}</swf>
<title>My application title</title>
<width>100%</width>
<height>100%</height>
<bgcolor>#ffffff</bgcolor>
</parameters>
</configuration>
</plugin>
and my WAR POM has the following:
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>4.1-beta</version>
<extensions>true</extensions>
<executions>
<execution>
<id>wrapper</id>
<phase>generate-resources</phase>
<goals>
<goal>wrapper</goal>
</goals>
<configuration>
<wrapperArtifact>
<groupId>${project.groupId}</groupId>
<artifactId>the-swc-project</artifactId>
<version>${project.version}</version>
</wrapperArtifact>
<htmlName>index</htmlName>
</configuration>
</execution>
<execution>
<goals>
<goal>copy-flex-resources</goal>
</goals>
</execution>
</executions>
</plugin>
Upon further investigation, I discovered that the problem here is that Flexmojos tries to reconfigure the WAR plugin, but it does so in a way that doesn't work with Maven 3.
The fairly grim workaround that I found involved manually configuring the WAR plugin as follows:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<warSourceExcludes>index.template.html</warSourceExcludes>
<webResources>
<resource>
<directory>target/war/work/wrapped-template</directory>
</resource>
</webResources>
</configuration>
</plugin>

Maven: how to get a war package with resources copied in WEB-INF?

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?
thanks,
rand
UPDATE:
in my pom now I use this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>war</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>myapp/target/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and I launch mvn with:
mvn -Dmaven.test.skip=true clean package resources:copy-resources
but I got:
[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'
[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<outputDirectory>VALUE</outputDirectory>
</configuration>.
[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<resources>VALUE</resources>
</configuration>.
I'm using maven 2.2 and the snippet basically is the same of the documentation
any idea?
either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory.
resource plugin
EDIT:
This configuration is working for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.
Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml
This configuration is working add plugin pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<!--copy resource file location-->
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</webResources>
<!--location for add file-->
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
</configuration>
</plugin>

Resources