maven-war-plugin does not change .war default directory - java

I try to change default .war directory by using maven-war-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<warSourceDirectory>${project.basedir}/src</warSourceDirectory>
</configuration>
</plugin>
But it does not work. In the sense of my .war creates in the defaul (target) directory.
I tried to use , but with the same result.
What am I do wrong?

You can use warSourceDirectory to configure additional directories to be packaged into the WAR file.
The outputDirectory property should do what you want to achieve:
The directory for the generated WAR.

Related

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

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>

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: how to include annotation-generated .java files in the .jar

I have some java 7 annotation processors (for xtend) on my class-path. Via some annotations they create java files.
This works great, in Elipse and in the Maven build.
The generated files end up in target/generated-sources/annotations as expected.
The corresponding generated .class files also end up where expected and are thus part of the final jar file.
Since I need to also include all java source files in my .jar file (there should be only one .jar file with the sources and classes) for GWT,
I have specified src/main/java as a resources dir (so that Maven copies the files to the classes dir and they end up in the jar file).
the trick with the resources directory does not really work for my generated files, because Maven will first copy all resources and then start the compilation (which in turn will generate the .java files via the annotation processors).
How can I tell Maven to copy also include the generated .java files in the .jar?
You can bind the maven-resources-plugin to the prepare-package phase to achieve copying annotation sources before packaging proper:
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-annotations</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>target/generated-sources/annotations</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
I hope that helps.
Cheers,

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