Web.xml is not getting included in War using mvn plugin - java

I am trying to include web.xml in war.
Here is how my pom.xml looks like, I have added maven-resources-plugin and maven-war-plugin to pom
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF/</directory>
<filtering>true</filtering>
<includes>
<include>web.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>${basedir}/war/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.3</version>
<configuration>
<filesets>
<fileset>
<directory>tomcat</directory>
</fileset>
<fileset>
<directory>war/WEB-INF/classes</directory>
</fileset>
<fileset>
<directory>war/WEB-INF/lib</directory>
</fileset>
</filesets>
</configuration>
</plugin>
But when I extract war here is what I get
/tomcat/work/Catalina/localhost/XProject/WEB-INF
/tomcat/work/Catalina/localhost/XProject/WEB-INF/classes
/tomcat/work/Catalina/localhost/XProject/WEB-INF/lib
What am I doing wrong ?

I have used multiple time maven-war-plugin. Here is the code snippet. This will include the web.xml if web.xml is present in your folder structure else ignore.
The problem which I think from your code snippet, may be issue with relative path for the web.xml. Use the below maven-war-plugin to resolve your issue
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

Add the warsourcedirectory tag.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

I have encountered the same problem while having the web.xml in the default path
src/main/webapp/WEB-INF/web.xml
but wasn't included in the .war.
Following #sitakant suggestion I removed the maven-resources-plugin from the pom.xml and web.xml is now included in the .war without further configurations.

Related

Reading properties files "inStream parameter is null"

I have read property files before and I realize there are many links on the topic but I think my properties file is not getting packaged correctly due to plug-ins. My Eclipse project has a properties file in src/main/resources called environment.properties. I have tried reading it through a method, even trying to declare the method as both static and non-static at different times:
static Properties properties = new Properties();
public void getClassPathProperties() {
LOGGER.debug("getClassPathProperties Enter");
try (final InputStream input = Utils.class.getClassLoader().getResourceAsStream("environment.properties")) {
properties.load(input);
LOGGER.debug("properties found:");
for (Object key : properties.keySet()) {
LOGGER.debug("" + ((String) key) + "=" + properties.getProperty((String) key));
}
} catch (Exception e) {
LOGGER.error("Unable to find environment.properties on classpath to Utils.class");
e.printStackTrace();
}
LOGGER.debug("getClassPathProperties Exit");
}
I tried the static declaration first but also tried non-static when a few posts mentioned it works until being declared as a static. I have also tried placing the environment.properties file manually within the the JAR file in different places without success. From Eclipse the exception is:
14:33:52.676 [main] ERROR com.goprecise.ams.handlers.utils.Utils - Unable to find environment.properties on classpath to Utils.class
java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:403)
and from the Maven command line an NPE on Properties.load() is reported (the LOGGER.error() message is from the code shown). The pom.xml is:
<?xml version="1.0"?>
... header ...
<properties>
<version.org.kie>7.48.0.Final-redhat-00004</version.org.kie>
<version.org.powermock>1.7.4</version.org.powermock>
<version.junit>4.12</version.junit>
<version.org.slf4j>1.7.26</version.org.slf4j>
<java.module.name>${project.name}</java.module.name>
</properties>
<dependencyManagement>
<dependencies>
... depeendencies
</dependencies>
</dependencyManagement>
<dependencies>
... many more dependencies
</dependencies>
<build>
<sourceDirectory>${project.build.directory}/generated-sources/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>*.png</include>
</includes>
</resource>
<resource>
<directory>${project.build.directory}/maven-shared-archive-resources</directory>
<filtering>true</filtering>
<includes>
<include>*.part</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<outputDirectory>${project.build.directory}/generated-sources/java</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-repository-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<resources>
<resource>
<directory>target/generated-sources/annotations</directory>
<includes>
<include>repoindex.html</include>
<include>*.wid</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>target/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessors>
<annotationProcessor>org.jbpm.process.workitem.core.util.WidProcessor</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-AwidName=${project.artifactId}</arg>
<arg>-AgenerateTemplates=true</arg>
<arg>-AgenerateWids=true</arg>
<arg>-AwidsResources=${project.artifactId}.wid:widtemplate.st</arg>
<arg>-AtemplateResources=kie-deployment-descriptor.xml:kie-ddtemplate.st,serviceinfo.json:serviceinfo.st,repoindex.html:repoindex.part,index.html:indextemplate.st,${project.artifactId}.bpmn2:defaultprocess.st</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<!-- root module has no assembly so ignore it -->
<ignoreMissingDescriptor>true</ignoreMissingDescriptor>
<descriptors>
<descriptor>${project.basedir}/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<executions>
<execution>
<id>integration-test-execution</id>
<phase>verify</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<builddir>${project.build.directory}</builddir>
</systemPropertyVariables>
<failIfNoTests>false</failIfNoTests>
<test>${it.test}</test>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<argLine>${failsafe.arg.line}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>project-sources-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>project-sources</id>
<goals>
<goal>archive</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
<argLine>-Xmx1024m -Dfile.encoding=UTF-8</argLine>
<systemPropertyVariables>
<apple.awt.UIElement>true</apple.awt.UIElement>
<org.uberfire.nio.git.daemon.enabled>false</org.uberfire.nio.git.daemon.enabled>
<org.uberfire.nio.git.ssh.enabled>false</org.uberfire.nio.git.ssh.enabled>
<org.uberfire.sys.repo.monitor.disabled>true</org.uberfire.sys.repo.monitor.disabled>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>default-jar</id>
</execution>
<!-- No OSGi manifestEntries for <goal>jar</goal>: if it supported, then felix has already added them -->
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
<excludes>
<exclude>**/logback-test.xml</exclude>
<exclude>**/jndi.properties</exclude>
</excludes>
<archive>
<manifestEntries>
<Bundle-SymbolicName>${java.module.name}.tests</Bundle-SymbolicName>
<Bundle-Version>
${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}.${osgi.snapshot.qualifier}
</Bundle-Version>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Vendor>${project.organization.name}</Bundle-Vendor>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>org.jbpm.contrib:template-resources:${version.org.kie}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I want the code in the JAR to find the properties at run-time: if the property file does not belong in src/main/resources (the typical place) where does it belong? If a plug-in is interfering with this how can I configure the plugin to enable my reading properties (or would simply moving the file enable it to be read)?
It's the declaration:
<resource>
<directory>src/main/resources</directory>
...
<includes>
<include>*.png</include> <!-- just this one isn't enough -->
</includes>
</resource>
If you (re-)declare the resources explicitely it's not cummulative with the defaults, i.e. the defaults are overriden. Have a look at your target/classes and you will find just PNG images there.
See resources:[testR|r]esources:
Always uses the project.build.[testR|r]esources element to specify the resources to copy.
and POM Reference, Super POM:
<project>
...
<build>
...
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
which means: Take all from these dirs by default but take nothing from these dirs if overridden (unless you include it when overriding).
So the property file also needs to be added to src/test/java because it is being called from test cases. Two separate JARs are actually created in the target directory:
ams-pam-workitemhandlers-2.0.1.1.jar
ams-pam-workitemhandlers-2.0.1.1-tests.jar
I think the application will find the resource file in src/main/resources or I will have to move it out but the test cases definitely find it in src/test/resources.

Maven assembly plugin is not including the project packages [duplicate]

This question already has answers here:
Building a fat jar using maven
(7 answers)
Closed 1 year ago.
I'm trying to build a fat jar with maven assembly plugin for distributing a desktop application.
My POM looks like
<build>
<!-- To parse properties files under resources folder : -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.acme.qpguard.editor.Application</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The command mvn assembly: single is creating a fat jar with other dependencies, but fat jar does no include my classes.
So while starting the jar, I'm getting an error
Error: Could not find or load main class com.acme.qpguard.editor.Application
How can I fix my POM so that it includes my project files too
Please note that the project is running fine in Eclipse.
Thanks, #Mayur and #Randy Casburn. Your pointers definitely helped me in finding a fix using maven shaded plugin.
I'm posting the fix as someone may find this useful at a later point of time
<build>
<!-- To parse properties files under resources folder : -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.acme.qpguard.editor.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: I have to apply a filter for removing signature files from some jars as it was breaking the execution.

Question: I want to delete directory from my maven project when i am running mvn eclipse:clean

This is what I have added in my pom.xml under suite tag. But when i run mvn eclipse:clean it successfully deletes .project and .setting files but not my Report directory.
<suite>
....
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>Reports</directory>
<includes>
<include>**</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
.....
</suite>
Please suggest if I am putting the plugin tags at wrong places.
Can you please try below if it helps
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>some/relative/path</directory>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
You forgot to close your XML Tags, here is the valid XML
<?xml version="1.0" encoding="UTF-8"?>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>Reports</directory>
<includes>
<include>**</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Property File Not Accessible from Jar built using Maven but working fine in eclipse?

I know people already asked similar questions but I tried all the suggestions given in other posts but didn't succeed. Hence posting my question:
I have a maven project with the configuration below:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>com.test.tools.services.Sample</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<excludes>
<exclude>config.properties</exclude>
<exclude>logback.xml</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/windows.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now my project structure is default maven project given below:
src
|main
java
com
test
tools
utils
Utility.java
|resources
config.properties
The maven build is making the zip file with the below structure:
target
|project.zip
|config
config.properties
|lib
main.jar
dependency.jar
The windows.xml defines the following:
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
</fileSets>
Inside the Utility.java I am accessing the property file as shown below:
InputStream input = Utility.class.getClassLoader().getResourceAsStream("config.properties");
The above code works fine when I run it in eclipse but the moment I run it on command prompt, I get File Not Found Exception.
Please provide any pointer/solution.
Thanks in advance.
`

Maven package resources with classes

I'm generating a War file with Maven and publishing the classes jar as well:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
Is it also possible to publish the resources from the war in the classes jar?
My advice would be to put these resources in their own separate jar module and then have the war use them as a dependency. That way the war and any other project could refer to the resources as if it was any other dependency.
But if you don't want to do that or you can't, I think you'll have to use a classifier. As I said, this isn't ideal. Here's a detailed tutorial on how to do it:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>only-library</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<classifier>only-library</classifier>
<excludes>
<exclude>**/Main*</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>everything</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<classifier>everything</classifier>
<includes>
<include>**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Categories