Use custom standalone-full.xml with arquillian - java

I'm using wildfly container with arquillian for my integration tests.
In some cases, I want to use JMS and standalone-full.xml with some custom configuration is loaded at server start.
So, for my int tests, I want to load this standalone-full.xml by putting it in src/test/resources.
How can i do that ?
I can't put the following line because it's the default jboss file and not my overrided standalone-full.xml file.
<property name="serverConfig">standalone-full.xml</property>
When I specify file path (in resources), it doesn't work.
<property name="serverConfig">src/test/resources/standalone-full.xml</property>
<property name="serverConfig">/src/test/resources/standalone-full.xml</property>
<property name="serverConfig">${project.basedir}/src/test/resources/standalone-full.xml</property>
[EDIT]
When I put maven variable in surefire-plugin like this :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<reuseForks>true</reuseForks>
<systemPropertyVariables>
<server.standalone.config.path>${project.basedir}/src/test/resources/standalone-full.xml</server.standalone.config.path>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
and use it in arquillian.xml
<property name="serverConfig">${server.standalone.config.path}</property>
I have this error :
java.lang.IllegalStateException: WFLYCTL0214: Could not get main file:
D:\my_project_path\int-tests/src/test/resources/standalone-full.xml. Specified
files must be relative to the configuration dir: D:\wildfly_path\wildfly-
10.1.0.Final\standalone\configuration

We use the following configuration:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources-wildfly-embedded</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<!-- the maven dependency plugin will have already downloaded the server on /target -->
<jboss.home>${dir.wildfly.home}</jboss.home>
<module.path>${dir.wildfly.modules}</module.path>
<!-- Do not use ./test/resources-wildfly/configuration because jboss will write to the
config directory and we don't want these files to change -->
<jboss.server.config.dir>${project.build.directory}/test-classes/configuration</jboss.server.config.dir>
<org.apache.deltaspike.ProjectStage>UnitTest</org.apache.deltaspike.ProjectStage>
<server-config>standalone.xml</server-config>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
Within src/test/resources-wildfly-embedded/configuration we have:
application-roles.properties
application-users.properties
logging.properties
mgmt-users.properties
mgmt-groups.properties
standalone.xml
It seems you need all these files for startup to work but there is no way to put standalone.xml in a different directory than the rest of the config.

Finally, I used another method because I have multiple modules…
The build process is broken down into three parts.
wildfly container deployment
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
[...]
<artifactItem>
<groupId>org.wildfly</groupId>
</artifactItem>
[...]
<execution>
<id>stop-test-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>go-offline</goal>
</goals>
</execution>
[...]
jboss_home and standalone-full.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Fork every test because it will launch a separate AS instance -->
<reuseForks>true</reuseForks>
<systemPropertyVariables>
<!-- the maven dependency plugin will have already downloaded the server on /target -->
<jboss.home>${project.build.directory}/${wildfly.test.embedded.folder}</jboss.home>
<server-config>standalone-full.xml</server-config>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
resources copy (jms.rar and custom standalone-full.xml
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-configuration-resource</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${wildfly.test.embedded.folder}/standalone/configuration</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/test/wildfly-resources/configuration</directory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-deployment-resource</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${wildfly.test.embedded.folder}/standalone/deployments</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/test/wildfly-resources/deployments</directory>
<includes>
<include>wmq.jmsra.rar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and in arquillian.xml:
<container qualifier="arquillian-wildfly-managed" default="true" mode="suite">
<configuration>
<property name="serverConfig">standalone-full.xml</property>
</configuration>
</container>
It works fine…

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-resources-plugin overwrite output directory not working

I have a spring boot application that serves vue static files as well. In the pom.xml I use maven-resources-plugin to copy the static files to resources folder. It works as intended the first time, but for some reason it does not overwrite the output directory when I change files the vue project and build the project again, even when I use <overwrite>true</overwrite>. When I delete the output dir manual and build again - it works fine.
My pom.xml:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id> Copy vue resources </id>
<phase> generate-resources </phase>
<goals>
<goal> copy-resources </goal>
</goals>
<configuration>
<overwrite> true </overwrite>
<outputDirectory> src/main/resources/public </outputDirecroty>
<resources>
<resource>
<directory> ${project.basedir}/../c4i-vue/target/dist </directory>
<includes>
<include>static/</include>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I also tried to put the <overwrite>true</overwrite> in the outside configuration but it didn’t work as well:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<overwrite> true </overwrite>
</configuration>
<executions>
<execution>
<id> Copy vue resources </id>
<phase> generate-resources </phase>
<goals>
<goal> copy-resources </goal>
</goals>
<configuration>
<outputDirectory> src/main/resources/public </outputDirecroty>
<resources>
<resource>
<directory> ${project.basedir}/../c4i-vue/target/dist </directory>
<includes>
<include>static/</include>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I would love some help, thank you!
It should be working fine..
Maybe the files were updated but the folders still have the last update date?

Bind a file inside a maven built EAR

Below is my project structure.
I require to copy a file from the location \MainProject\src\non-packaged-resources to the \MainProject\sub-project2\sub-project2-web\src\main\resources location on building the MainProject.
Below is the build section of the pom file of my sub-project2-web but the file is not copied while building.
<build>
<finalName>sub-project2-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>**/.copyarea.db</warSourceExcludes>
<packagingExcludes>**/.copyarea.db</packagingExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When observing the build log, it skips the file being copied with the below error
skip non existing resourceDirectory
But the maven build gets successful.
Goal is to bind the file which exists inside the \MainProject\src\non-packaged-resources to all the ears (sub-project1.ear, sub-project2.ear) on building the maven project. First I am trying to test this in the sub-project2-web only.
Please advice on how to provide correct paths based on this requirement.
I am using maven 2.2.1 version (project is built on this.)
Define the paths with reference to your project base directory as follows.
<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
<directory>${project.basedir}/src/non-packaged-resources</directory>
Then add it in the pom.xml file in the maven-resources-plugin.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Based on the above answer I found the correct way to get the locations of the parent and the base directories. Below worked for me.
<build>
<finalName>sub-project2-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>**/.copyarea.db</warSourceExcludes>
<packagingExcludes>**/.copyarea.db</packagingExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.parent.parent.basedir}/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Use variables from properties file in a Maven Plugin

I need to be able to use "variables" for a maven plugin like this:
<plugin>
<groupId>com.jelastic</groupId>
<artifactId>jelastic-maven-plugin</artifactId>
<version>1.8.4</version>
<configuration>
<api_hoster>${api_hoster}</api_hoster>
<email>${email}</email>
<password>${password}</password>
<environment>${environment}</environment>
<!--<context>[specify the context if you need it]</context>-->
<!--<comment>[insert comment if you need it]</comment>-->
</configuration>
</plugin>
Already have the properties file set in the base directory and have used the plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/jelastic.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Still, the variables in the plugin cannot be resolved, what could be wrong here?
You must either declare those variables as <properties> in project or profile or pass them as env variables like mvn whatever -Dyourprop=value
Read about properties: https://maven.apache.org/pom.html#Properties
Read about profiles: https://maven.apache.org/guides/introduction/introduction-to-profiles.html
add those lines to your POM:
<build>
<resources>
<resource>
<includes>
<include>**/*.properties</include>
</includes>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
And this properties file:
api_hoster:${api_hoster}
email:${email}
password:${password}
environment:${environment}
and invoke any maven plugin by mentioning a profile:
mvn clean -Pname

Program is not running when try to put properties file outside of jar using maven

I have a maven project in eclipse using m2e. I am suing spring. When maven build the jar file then i copy all the dependencies jars to a folder dependency-jars. Here how i am doing it.
<build>
<!-- to avoid maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e error -->
<pluginManagement>
<plugins>
<!-- Ignore/Execute plugin execution -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<!-- copy-dependency plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- Maven compiler plugin
If you run the code maven package now, Maven will package this Java project into a jar file
named “test-1.0.0.jar“, in target folder.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<!-- To make jar file like a exe file, you need to define a manifest file and declare the application
entry point inside via maven-jar-plugin in pom.xml.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!-- Manifest specific configuration -->
<manifest>
<!-- Classpath is added to the manifest of the created jar file. -->
<addClasspath>true</addClasspath>
<!--
Configures the classpath prefix. This configuration option is
used to specify that all needed libraries are found under dependency-jars/
directory.
Use “classpathPrefix” to specify folder name in which all properties will be placed.
-->
<classpathPrefix>dependency-jars/</classpathPrefix>
<!-- Specifies the main class of the application -->
<mainClass>pk.training.basitMahmood.BatchImport</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- uses maven-dependency-plugin to copy all dependencies to "target/dependency-jars/" folder, and
defines the dependency classpath with maven-jar-plugin
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>
log4j, org.slf4j, org.springframework, commons-net, commons-collections,
org.apache.commons, javax.mail, org.apache.velocity, commons-logging
</includeGroupIds>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Till things are ok. Now i have 2 properties file, that i don't want to include in JAR, because i don't want that each time i have to make jar after changing properties file. I want that properties files are out of the jar, and their entry are added in the Manifest, and each time i execute the jar, it simply reads properties form the file out side of the jar. For that i tried some thing like below. But i stuck how i define path in the <manifest>, where i have already defined path for the dependency-jars. Here what i did, but then my program is not running.
<build>
<!-- To exclude any file from a jar / target directory you can use the <exludes> tag in your pom.xml.
all files with extention .properties will not be included:
-->
<resources>
<resource>
<directory>${basedir}/src/main/java/pk/training/basitMahmood/util</directory>
<filtering>false</filtering>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/email</directory>
<filtering>false</filtering>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
<!-- to avoid maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e error -->
<pluginManagement>
<plugins>
<!-- Ignore/Execute plugin execution -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<!-- copy-dependency plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
<!-- maven-antrun-plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- Maven compiler plugin
If you run the code maven package now, Maven will package this Java project into a jar file
named “test-1.0.0.jar“, in target folder.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<!-- To make jar file like a exe file, you need to define a manifest file and declare the application
entry point inside via maven-jar-plugin in pom.xml.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!-- Manifest specific configuration -->
<manifest>
<!-- Classpath is added to the manifest of the created jar file. -->
<addClasspath>true</addClasspath>
<!--
Configures the classpath prefix. This configuration option is
used to specify that all needed libraries are found under dependency-jars/
directory.
Use “classpathPrefix” to specify folder name in which all properties will be placed.
-->
<classpathPrefix>dependency-jars/</classpathPrefix>
<!-- Specifies the main class of the application -->
<mainClass>pk.training.basitMahmood.BatchImport</mainClass>
</manifest>
<!-- Use “Class-Path” to specify the folder. “.” Indicate current folder, while
“propertiesFiles” specifies “propertiesFiles” folder in same directory as JAR.
-->
<manifestEntries>
<Class-Path>.propertiesFiles</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- uses maven-dependency-plugin to copy all dependencies to "target/dependency-jars/" folder, and
defines the dependency classpath with maven-jar-plugin
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>
log4j, org.slf4j, org.springframework, commons-net, commons-collections,
org.apache.commons, javax.mail, org.apache.velocity, commons-logging
</includeGroupIds>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- Move property files out of JAR and put in a directory say “target/properties-files” -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven-antrun-plugin.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="target/properties-files" overwrite="true">
<fileset dir="src/main/java/pk/training/basitMahmood/util">
<include name="*.properties"/>
</fileset>
<fileset dir="src/main/resources/email">
<include name="*.properties"/>
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
After using this configuration when i try to run the program, then i get the error that
IOException parsing XML document from class path resource
[spring/app-context-xml.xml]; nested exception is
java.io.FileNotFoundException: class path resource
[spring/app-context-xml.xml] cannot be opened because it does not exist
Sometimes i get the error that
java.lang.NoClassDefFoundError: pk/training/basitMahmood/BatchImport
Caused by: java.lang.ClassNotFoundException: pk.training.basitMahmood.BatchImport
While with the previous configuration(i.e., only jar copying) everything works fine
What i am doing wrong ? How can i achieve what i want ? Please help
Thanks
I had the same problem time ago and I resolved it using maven-shade-plugin.
My sample:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${final.package.name}</finalName>
<artifactSet>
<excludes>
</excludes>
</artifactSet>
<filters>
<filter>
<artifact>*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/*.INF</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<mainClass>mainClass</mainClass>
<Version>${project.version}</Version>
<Codename>codename</Codename>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</plugin>

Categories