How to include empty directories while building using spring maven plugin? - java

my project has an empty folder 'static' under src/main/resources and while using mvn spring-boot:repackage 'static' folder hasn't copied to 'target' folder, but whenever 'static' folder contains any file like 'src/main/resources/static/images/asb.jpg' then the file and it's parent directories are copied to 'target'. i've gone through spring-boot-maven-plugin docs but didn't found any solution.
I've noticed that maven-build-plugin has a configuration like below, that can copy empty foldes to target. but didn't find any solution for spring-boot-maven-plugin.
<configuration>
<includeEmptyDirectories>true</includeEmptyDirectories>
</configuration>

It seems not possible with just spring-boot-maven-plugin. But you can include in your pom.xml the Maven Resources Plugin and use its includeEmptyDirs configuration property:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
</plugin>

Related

generated sources of other modules

I have a multi-module maven project. I use maven build helper plugin to automatically add generated sources to the classpath.
I am able to use the generated sources of module-X in module-X, however, when I add module-X as a dependency to module-Y, the generated sources of module-X are not visible because they are not included in the X.jar file.
Is there a way to include the generated sources in the jar file or force maven to generate sources of dependencies?
You can explicitly specify that the generated classes should be part of the output jar file:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<includes>
<include>generatedClassesFolderPath</include>
</includes>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Replace the generatedClassesFolderPath with the relative path of the folder where the generated classes are.
More info:
How to include/exclude content from jar artifact
I had the same question and I solve it as follows:
Add an application class under package such as src/main/java/com..... in your module-X and add a #SpringBootApplication annotation. In addition to this, the application class can be no content.
Make sure module-X in module-Y dependencies and restart `module-Y.

Building .war file using maven. Missing files and directories

I try to build .war file by using maven plugin:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
My project structure looks like:
/main/webapp/WEB-INF/
/main/webapp/WEB-INF/spring/...
/main/webapp/WEB-INF/web.xml
/main/webapp/public/...
/main/webapp/resources/...
/main/webapp/views/...
After building, my war file contains only WEB-INF and META-INF. All other content of webapp directory is missing (public, resources and views). Furthermore the WEB-INF dir in .war file consists only /classes and /lib directories (/WEB-INF/spring and WEB-INF/web.xml are missig).
How to tell maven to pack all webapp and WEB-INF directory content into war file?
There is a mismatch between your configuration of the maven-war-plugin and the structure of your project. With <warSourceDirectory>WebContent</warSourceDirectory>, you are configuration the maven-war-plugin to look for your webapp sources inside the WebContent directory. However, your sources are in main/webapp.
I suggest you move all of your webapp sources inside src/main/webapp (instead of main/webapp) and update the maven-war-plugin configuration to:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
By default, the maven-war-plugin will be looking for your webapp sources inside src/main/webapp.
You can package your project using Maven by using following command in your command-line.
mvn clean package
Take a look at the Maven layout definition: The webapp folder should be in src/main/webapp instead of main/webapp.
Alternatively you can also force maven to look in a different directory for your resources. See https://maven.apache.org/pom.html#Build_Settings

Obfuscating WAR file with Proguard

I want to obfuscate my web application built as WAR archive, as this sensitive application in first time deployed outside our data center. I tried to use the Proguard GUI tool to obfuscate the input war, with all the service jar required for the UI application, with other external dependencies. Though the Proguard runs successfully with some warnings, ex., duplicate definition of library class [javax.servlet.UnavailableException], the output war contains no classes, but has lib with the library jars and web.xml files. Any steps I mess? Any right document on this? I would appreciate if anyone can provide the right document or steps to successfully obfuscate a WAR file with dependent project (a .jar file) and other external jar files (that needs no obfuscation).
you wouldn't obfuscate a war but rather the jars your using. What you can do here is setup your project so the project that makes up the war - configuration xml, WEB-INF content, resources and the web content and servlet definitions and put your java in a library project. Obfuscate the library project and use those obfuscated jars in your web project.
That's what I do, hope it helps.
Protector4j is the best solution to obfuscate the war file, due to graphic user interface its too easy to use and their eclipse plugin is also available.
You will download it from this link
https://doc.protector4j.com/protect-tomcat-web-app
I have done the same way. I used the below url for code obfuscation and i am successful.
http://bratonfire.blogspot.com/2012/01/war-file-obfuscation-using-proguard.html
I created a new folder and redirected output of classes to this folder. But the strange thing is that i am able to see the .java and .class files in the two locations. I am also worried about recreating a war file. can someone mention the clear and detailed steps.
Thanks,
Rahul
We also have the same issue and need to obfuscate all classes packaged in war file.Here is the approach that I followed.
Firstly we need to set order of plugins **(compiler, proguard, war)**declared in pom.xml file as below.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.14</version>
<configuration>
</configuration>
<executions>
<execution>
<!-- Dont worry about compiler error. For first time, change this value to package so that plugin installs successfully. -->
<phase>process-classes</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<warName>mfs-transaction-management</warName>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Exclude your default packages from war packaging. Do not include "**" in double quotes in actual code -->
<packagingExcludes>
WEB-INF/classes/com/package/mypackage1/"**",
WEB-INF/classes/com/package/mypackage2/"**",
</packagingExcludes>
<webResources>
<webResource>
<directory>${project.build.directory}/proguardClasses</directory>
<targetPath>WEB-INF/classes</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>`
Then create a file proguard.conf under the root of your project at the same level where pom.xml is placed.
Add your own configuration regarding proguard in the file and the add below two lines in this file to tell input and output folder to proguard plugin.
You need to set paths according to your project structure in these lines
-injars 'C:\Users\Rajdeep\git\dfs-core\mfs-transaction-management\target\classes'
-outjars 'C:\Users\Rajdeep\git\dfs-core\mfs-transaction-management\target\proguardClasses'
Apart from this you need to install proguard-base manually in maven repository using mvn install command.
Provide your own groupid, artifact and version and made same changes to pom.
It is proguard.jar found under proguard6.0.3\lib folder when you download proguard manually.
I think everything will be ok and now when you run mvn clean package, your war file should included obfuscated class files.
Use Proguard GUI to obfuscate war files.
Once you run proguardgu.bat or proguardgui.sh file from bin folder of your proguard directory. You can select wars by clicking Input/output menu.

Maven - package linked resources

I have a project that has linked resources to it. These linked resources are provided in maven Pom file like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<linkedResources>
<linkedResource>
<name>src/main/webapp/Manual.pdf</name>
<type>1</type>
<location>C:/location/Manual.pdf</location>
</linkedResource>
....
</plugin>
on execution mvn eclipse:eclipse , these resources now appear under my eclipse working tree dir (as well as inside .project properties file). However how do i package these resources using mvn package, so they appear in my war file?.
Thanks

Maven is creating a war using the wrong classes directory, or it is putting the compiled classes in the wrong place

MyProject > src
> target
> MyProject > classes (1)
> classes (2)
The actual newly compiled code is being put into 2, however the war file is getting created using 1. Thus my war file is not up to date at all.
What should I be doing ? Shown below snippet of my pom ... :
<build>
<finalName>MyProject</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
<warName>MyProject</warName>
</configuration>
<version>2.1.1</version>
</plugin>
</plugins>
</build>
target/PROJ is nothing but exploded war.
try deleting target directory. Perform maven build from commandline. and check if all things are created as they are at this point. If so, then maven is behaving correctly as both of the places contains updated class files and that target/PROJ directory is exploded war directory. You can use path to that directory to setup a context in tomcat/web container.
I am a bit in doubt on what you are showing.
Normally in war project, the target should be something like
PROJ
+ target
+ classes
+ PROJ-version
+ WEB-INF
+ classes
I have never see a target/PROJ in Maven WAR project
If what I am showing is what you seen, then it is what Maven always do.
Source are compiled and put in classes.
During creation of WAR, different files, including the project's target/classes, will be copied to PROJ-version directory which form the structure of WAR, and it will be used to create the WAR file.
Therefore there should be no problem that Maven is using target/PROJ-version/WEB-INF/classes in creating the WAR, as it is getting copied from target/classes when you are building.
If it is not your case, probably I will suggest you share your POM. The directory you mentioned doesn't seems reasonable for a WAR project.

Categories