mvn war:war without copying resources - maven

I have a file in a Maven project that need to be obsfuscated. At the moment I:
Clean/build the project;
Open an eternal application to obsfuscate the file in the /target/ROOT/blah folder;
I then want to run mvn war:war but it always copies the resources folder back into the /target/ROOT folder which overwrites the obsfuscated file.
MVN output
Packaging webapp
Assembling webapp [core] in [core\target\ROOT]
Processing war project
Copying webapp resources [src\main\webapp] <= I THINK THIS IS THE PROBLEM
Webapp assembled in [22812 msecs]
Building war: target\ROOT.war
I have tried the following and a few variations:
<profile>
<id>war-only</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceExcludes>**</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Any suggestions please?

OK got it eventually:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>${basedir}/src/main/null</warSourceDirectory>
</configuration>
</plugin>
Setting warSourceDirectory to any non-existant directory meant it didn't copy anything in.

Related

How to place output Jar and War files in another specified folder(it may be outside of the project)?

i want to place output jar or war in another separate folder which should contain only jar or war file. i.e folder may be outside of the project. is that possible?
Although generally it's not a good idea to deviate from Maven conventions, you can use outputDirectory parameter in maven-jar-plugin to specify a directory other than ${project.build.directory}
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<outputDirectory>path/to/your/folder</outputDirectory>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>

Openshift 3 WAR

In the Openshift 2 I had such a profile in a pom.xml file:
<profile>
<!-- openshift red hat cloud build profile -->
<id>openshift</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<outputDirectory>webapps</outputDirectory>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
and this was responsible for putting a WAR file to directory from where it was automatically deployed to Tomcat-like-jboss.
Now - in Openshift 3 - by using browser-embeded ssh console I checked that WAR files were build and put into /tmp/src/webapps directory. Where should I move it (how should I modify the Maven profile) to make new Openshift 3 Tomcat-like-jboss deploy it and host it?
I've found the answear - the correct outputDirectory is target, so the WAR plugin looks now:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<outputDirectory>target</outputDirectory>
<warName>ROOT</warName>
</configuration>
</plugin>
I've found it there: https://github.com/gshipley/book-helloworld/blob/master/pom.xml - in a sample OpenShift app. Now my WAR are being deployed to the WildFly!
Moreover - this free e-book is really heplful: https://www.openshift.com/promotions/for-developers.html.

I don't want to include external jar in my maven war

I don't want to include external jars when I build a war.What should I do?
Though not sure about why are you thinking to exclude the jar files, but yes you can do that if you are using maven-war-plugin
This will work out
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
This a whole bunch of configuration plugin that excludes all .jar files.
This source explains about that and even regex patterns that can be used.

Configure webXml in IntelliJ maven-war-plugin

In the Maven Projects tools window, I see that the maven-war-plugin:2.1.1 is installed but this is grayed out. When I run the war:war goal, I get the following warning:
[INFO] Building war:
C:\Users\Klaus\IdeaProjects\JerseyTestRestWebApp\target\tut-test-rest-api.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which
will be ignored (webxml attribute is missing from war task, or
ignoreWebxml attribute is specified as 'true
I am using IntelliJ 14.1. How does one go about setting this parameter?
Try adding this as your build configuration and it should clear up the error your seeing.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

Use Maven to Copy Files to WEB-INF Before WAR is Packaged

I have two servlet config files in my webapp, one for our normal environment (Heroku) and the other for WebLogic. I have a Maven profile for our WebLogic builds that should copy "servlet-context.xml.weblogic" to "servlet-context.xml". Everything appears to be working, except that the copy takes place AFTER the war file is built, so the correct servlet context doesn't get included in the package. What is the right build phase to use in the maven-antrun-plugin to get the copying done correctly?
Here is the relevant section of my POM.xml file:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<move
file="${project.build.directory}/${project.build.finalName}/WEB-INF/spring/appServlet/radio-context.xml.weblogic"
tofile="${project.build.directory}/${project.build.finalName}/WEB-INF/spring/appServlet/radio-context.xml"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
This fails with the following error:
Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (default) on project radio: An Ant BuildException has occured: Warning: Could not find file C:\workspace\radio\target\radio-1.0.0-BUILD-SNAPSHOT\WEB-INF\spring\appServlet\radio-context.xml.weblogic to copy. -> [Help 1]
However, if I change the <phase> to package, the copy works, but after the war is built.
Any help would be appreciated.
As a reference, this page lists the Maven lifecycle phases.
The key phases you need to think about in your build are:
process-resources - where most of the files are placed into ${project.build.directory}
package - this is the phase where the WAR is built
BUT...
Looking at the documentation of the WAR plugin, the copying of WAR resources to ${project.build.directory}/${project.build.finalName} occurs in the war:war goal, which executes in the package phase also.
So let's take a step back. What you want to achieve is to use a different radio-context.xml file depending on your profile. Perhaps a better approach would be to configure the WAR plugin differently in your weblogic profile.
Have a separate webapp resource directory for your weblogic, put your custom radio-context.xml file in there, and only include that directory in the weblogic profile. e.g.
<project>
...
<profiles>
<profile>
<id>weblogic</id>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp-weblogic</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Try using process-resources, this copies your file before the war is built.

Resources