POM How to include properties file inside a jar. The pom also creates a war - maven

I developed a pom that creates a war file and a jar file. How do I insert a properties file into the JAR(not the war)? The file I need to insert into the JAR is located in the directory, properties/its, within the project. You can see in the pom below in the maven-resources-plugin where I have attempted to add the properties file in the package phase with the associated jar goal. Then I tried again in the maven-jar-plugin, using the process-resources phase and jar goal.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxx.xx.Lookup</groupId>
<artifactId>lookup</artifactId>
<packaging>war</packaging>
<version>${env.ENVIRONMENT}</version>
<name>elookup</name>
<url>http://maven.apache.org</url>
<description>Lookup</description>
<dependencies>
<dependency>
<groupId>xxx.xxx.utils</groupId>
<artifactId>Utils</artifactId>
<version>${env.ENVIRONMENT}</version>
</dependency>
</dependencies>
<parent>
<groupId>xxx.xx.parent_project</groupId>
<artifactId>xxxProject</artifactId>
<version>master-version-1.0</version>
<relativePath>../Project/pom.xml</relativePath>
</parent>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>${env.ENVIRONMENT}</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>${env.ENVIRONMENT}</env>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<env>${env.ENVIRONMENT}</env>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>properties/its</directory>
<!-- override the destination directory for this resource -->
<targetPath>properties/its</targetPath>
</resource>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/META-INF</directory>
<!-- override the destination directory for this resource -->
<targetPath>META-INF</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/WEB-INF</directory>
<!-- override the destination directory for this resource -->
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>images</directory>
<!-- override the destination directory for this resource -->
<targetPath>images</targetPath>
</resource>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>style</directory>
<!-- override the destination directory for this resource -->
<targetPath>style</targetPath>
</resource>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>html</directory>
<!-- override the destination directory for this resource -->
<targetPath>.</targetPath>
</resource>
</webResources>
<packagingExcludes>WEB-INF/lib/stax-api-1.0.1.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
<encoding>UTF-8</encoding>
<executions>
<execution>
<id>make-a-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<resources>
<resource>
<directory>properties/its</directory>
<includes>
<include>**/*</include>
</includes>
<!-- <targetPath>.</targetPath> -->
</resource>
</resources>
</execution>
</executions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-a-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>jar-resources</id>
<phase>process-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>properties/its</directory>
<targetPath>.</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptor>src/main/assembly/jar.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>../../Project/workspace/src/main/filters/eddb.${env.ENVIRONMENT}.properties</filter>
</filters>
</build>
</project>

How do I insert a properties file into the JAR(not the war)? The file I need to insert into the JAR is located in the directory, properties/its, within the project.
Before package the jar use maven-antrun-plugin to copy your file into the same directory the maven-jar-plugin uses as source directory for jar-ing.

Related

Include xml files in maven project

I have a maven pom file to build from a structure like:
package1
--1.java
--2.java
--packageMetaInfo.xml
package2
--21.java
--22.java
--packageMetaInfo.xml
When I do a maven compile, the xml files don't come in the target.
maven-compiler-plugin 3.5.1 - Unless I exclude the xmls through <exclusions>, I get an error that "Fatal error compiling: All compilation units must be of SOURCE kind ->"
maven-compiler-plugin 2.0.1 - compiles but skips the xmls
Is there a way I can have the xmls included in my jar. The structure would be
x.jar
package1
--1.class
--2.class
--packageMetaInfo.xml
package2
--21.class
--22.class
--packageMetaInfo.xml
*I understand it may not be maven standard to have xml with source files but I am working on a specific product and need to maintain this structure in both input and output.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.incent</groupId>
<artifactId>Release-4.3.0.1.0.0001</artifactId>
<name>AAA Custom code</name>
<version>2.4.0.1</version>
<properties>
<ormb.cmccb.path>./Active_Repository/CMCCB</ormb.cmccb.path>
<ormb.customcode.path>${ormb.cmccb.path}/data</ormb.customcode.path>
<ormb.release.name>AAA-4.3.0.1.0.0001</ormb.release.name>
<ormb.target.path>./target</ormb.target.path>
<ormb.output.path>Release-${ormb.release.name}/Application/${ormb.release.name}/CMCCB</ormb.output.path>
<ormb.serverfile.output.relpath>./target/server</ormb.serverfile.output.relpath>
<build.number>SNAPSHOT</build.number>
</properties>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>icu4j</groupId>
<artifactId>icu4j</artifactId>
<version>49.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>wlfullclient</groupId>
<artifactId>wlfullclient</artifactId>
<version>10.3.4.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>${ormb.customcode.path}/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-cm</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${ormb.customcode.path}/etc/lib</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${ormb.target.path}</directory>
<includes>
<include>cm.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-mwpackage</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${ormb.serverfile.output.relpath}</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${ormb.target.path}</directory>
<includes>
<include>Release-${ormb.release.name}.zip</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<encoding>ISO-8859-1</encoding>
<source>1.7</source>
<target>1.7</target>
<includes>
<include>**/cm/**</include>
</includes>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
<resources>
<resource>
<directory>${ormb.customcode.path}/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>create-cm</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>Active_Repository/assembly/executable.xml</descriptor>
</descriptors>
<finalName>cm</finalName>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<archive>
<manifestEntries>
<Specification-Title>${project.name}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Implementation-Version>${build.number}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</execution>
<execution>
<id>create-distro</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>Active_Repository/assembly/dist.xml</descriptor>
</descriptors>
<finalName>custom-action-dist</finalName>
<appendAssemblyId>false</appendAssemblyId>
<finalName>Release-${ormb.release.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I faced the same issue, I wanted to keep fxml & java files in the same folder (I'm using scenebuilder who needs both fxml and java files at the same place).
Here is my solution in pom.xml:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
If you want the xml files to be included in your JAR file, then store them in the resources folder, instead of java.
Presumably they're in src/main/java. Try moving them to src/main/resources. Anything in src/main/resources will be packaged into the JAR file along with the .class files.

Deploy as ROOT and SpringLoaded Hippo CMS

Spring Loaded has been working fine for me until I recently switched to deploy as root.
(to completely get rid of the "/site" in the URLs of my website)
I've modified the original config brought up by Jeroen here but it's not working.
(The files under ${project.basedir}/target/tomcat7x/webapps/ROOT is not updated and the website is referring to this outdated source instead of the up-to-date ${project.basedir}/site/target/ROOT)
What am I missing?
My ${project.basedir}/pom.xml:
<profile>
<id>cargo.run</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-tomcat-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/contexts</outputDirectory>
<resources>
<resource>
<directory>conf</directory>
<includes>
<include>*-context.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<configuration>
<properties>
<cargo.jvmargs>-Xmx1920m -Xdebug -Xrunjdwp:transport=dt_socket,address=${cargo.debug.address},server=y,suspend=${cargo.debug.suspend} -noverify -javaagent:/Users/eric/libs/springloaded.jar ${cargo.jvm.args}</cargo.jvmargs>
</properties>
<configfiles>
<configfile>
<file>${project.build.directory}/contexts/site-context.xml</file>
<todir>conf/Catalina/localhost/</todir>
<tofile>site.xml</tofile>
</configfile>
</configfiles>
</configuration>
</configuration>
</plugin>
...
</plugins>
</build>
</profile>
My ${project.basedir}/site/pom.xml
<finalName>ROOT</finalName>
...
<plugin>
<groupId>com.googlecode.mavenfilesync</groupId>
<artifactId>maven-filesync-plugin</artifactId>
<configuration>
<mappings>
<mapping>
<sourceFolder>src/main/resources</sourceFolder>
<destinationFolder>#../target/tomcat${cargo.tomcat.major.version}x/webapps/site/WEB-INF/classes</destinationFolder>
</mapping>
<mapping>
<sourceFolder>src/main/webapp</sourceFolder>
<destinationFolder>#../target/tomcat${cargo.tomcat.major.version}x/webapps/site</destinationFolder>
</mapping>
</mappings>
</configuration>
</plugin>
${project.basedir}/conf/site-context.xml (I've tried having both path as empty string and "/" and neither works)
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/" docBase="${project.basedir}/site/target/ROOT">
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" searchVirtualFirst="true"
virtualClasspath="${project.basedir}/site/target/classes" />
</Context>
Because you renamed the deployed application to ROOT you might need to also change the name of the site-context.xml to ROOT.xml. According to the Tomcat context docs it's required to match the war files name.
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<configuration>
<properties>
<cargo.jvmargs>-Xmx1920m -Xdebug -Xrunjdwp:transport=dt_socket,address=${cargo.debug.address},server=y,suspend=${cargo.debug.suspend} -noverify -javaagent:/Users/eric/libs/springloaded.jar ${cargo.jvm.args}</cargo.jvmargs>
</properties>
<configfiles>
<configfile>
<file>${project.build.directory}/contexts/site-context.xml</file>
<todir>conf/Catalina/localhost/</todir>
<tofile>ROOT.xml</tofile>
</configfile>
</configfiles>
</configuration>
</configuration>
</plugin>

Not getting values from properties files in context.xml file

I am unable to retrieve value from .properties file into my context.xml
pom.xml ( didn't mention dependencies )
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>myproj</artifactId>
<version>0.1.1</version>
<packaging>war</packaging>
<name>myproj</name>
<url>http://maven.apache.org</url>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
<build.number>0</build.number>
<svn.revision.number>0</svn.revision.number>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/environment</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<printSummary>false</printSummary>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<excludes>
<exclude>**/*_Roo_*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<additionalBuildcommands>
<buildCommand>
<name>org.eclipse.ajdt.core.ajbuilder</name>
<arguments>
<aspectPath>org.springframework.aspects</aspectPath>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
</buildCommand>
</additionalBuildcommands>
<additionalProjectnatures>
<projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
<projectnature>com.springsource.sts.roo.core.nature</projectnature>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warName>myproj</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
context.xml (inside src/main/webapp/META-INF)
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/mydb" auth="Container"
type="javax.sql.DataSource" username="${database.usernameee}" password="${database.password}"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sybase://localhost:9000/common_db"
maxActive="10" maxIdle="4" />
</Context>
I have some .properties files inside under:
src/main/resources
src/main/environment/dev
I am unable to get values inside my context.xml of ${database.usernameee} and ${database.password}
please suggest what went wrong?
Do the following:
1) Remove the <resources> section. It's not needed for the filtering you want to do and src/main/resources is the default - it doesn't need to specified separately.
<build>
<!-- Delete the <resources> section -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/environment</directory>
<filtering>false</filtering>
</resource>
</resources>
2) Add a <filters> element under <build> specifying the environment specific properties file
<build>
<filters>
<filter>src/main/environment/${env}/some.properties</filter>
</filters>
...
The above assumes your properties files are called some.properties - so change this to the real name of your file.
3) Modify the configuration of the maven-war-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/META-INF/context.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<warName>myproj</warName>
</configuration>
</plugin>
That will now filter the context.xml using the environment specific properties - depending upon what profile you used.
(note that you may have a typo in 'database.usernameee' - as it has extra ee at the end)
If you want to get your Maven properties from external files (not from properties inside POM's), you should use Maven Filters: see here and here.

Maven property not set when using profile

The filtering via maven package -Pexplode is not working. "${ds.xml}" is not being replaced with "openmind-ds.xml". The value "openmind-ds.xml" and key "ds.xml" are in the filter.properties. Also ${as.deploy}.
Error Message
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (package) on project openmind-ear: An Ant BuildException has occured: Warning: Could not find file /home/localhost/workspaceOM/openmind/openmind-ear/target/${ds.xml} to copy. -> [Help 1]
Mode Debug
[INFO] Executing tasks
[DEBUG] getProperty(ns=null, name=ant.reuse.loader, user=false)
[antlib:org.apache.tools.ant] Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be found.
[DEBUG] getProperty(ns=null, name=as.deploy, user=false)
Property "as.deploy" has not been set
Setting project property: deploy-path -> ${as.deploy}/openmind-ear.ear
[DEBUG] getProperty(ns=null, name=deploy-path, user=false)
[echo] Exploding to ${as.deploy}/openmind-ear.ear
[DEBUG] getProperty(ns=null, name=deploy-path, user=false)
[delete] Directory /home/localhost/workspaceOM/openmind/openmind-ear/${as.deploy}/openmind-ear.ear cannot be removed using the file attribute. Use dir instead.
[DEBUG] getProperty(ns=null, name=deploy-path, user=false)
[mkdir] Skipping /home/localhost/workspaceOM/openmind/openmind-ear/${as.deploy}/openmind-ear.ear because it already exists.
[DEBUG] getProperty(ns=null, name=deploy-path, user=false)
[sync] PASS#1: Copying files to /home/argonist/om-umgebung/workspaceOM/openmind/openmind-ear/${as.deploy}/openmind-ear.ear
pom.xml in ear-project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.openmind</groupId>
<artifactId>openmind</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>openmind-ear</artifactId>
<packaging>ear</packaging>
<name>${project.artifactId} : ${project.version} EAR</name>
<profiles>
<profile>
<id>env-dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>env-prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
<profile>
<id>explode</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<configuration>
<tasks>
<property name="deploy-path" value="${as.deploy}/${app.context}-ear.ear" />
<echo>Exploding to ${deploy-path}</echo>
<delete file="${deploy-path}" quiet="true" />
<mkdir dir="${deploy-path}" />
<sync todir="${deploy-path}" verbose="true">
<fileset
dir="${project.build.directory}/${project.build.finalName}" />
</sync>
<copy todir="${as.deploy}" file="${project.build.directory}/${ds.xml}"
verbose="true" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>unexplode</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>clean</id>
<phase>clean</phase>
<configuration>
<tasks>
<property name="deploy-path" value="${as.deploy}/${app.context}-ear.ear" />
<echo>Unexploding: ${deploy-path}</echo>
<delete file="${deploy-path}" quiet="true" />
<delete dir="${deploy-path}" quiet="true" />
<delete file="${as.deploy}/${ds.xml}" quiet="true" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
.....
</dependencies>
<build>
<finalName>${app.context}-ear</finalName>
<filters>
<filter>../src/main/filters/filter-${env}.properties</filter>
</filters>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultJavaBundleDir>lib/</defaultJavaBundleDir>
<version>6</version>
<archive>
<manifestEntries>
<Build-Date>${timestamp}</Build-Date>
<Build-Revision>${buildNumber}</Build-Revision>
<Mode>${env}</Mode>
</manifestEntries>
</archive>
<modules>
<jarModule>
<groupId>org.jboss.el</groupId>
<artifactId>jboss-el</artifactId>
<includeInApplicationXml>false</includeInApplicationXml>
<bundleDir>lib</bundleDir>
</jarModule>
<jarModule>
<groupId>${project.groupId}</groupId>
<artifactId>${app.context}-datamodel</artifactId>
<includeInApplicationXml>false</includeInApplicationXml>
<bundleDir>lib</bundleDir>
</jarModule>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>${app.context}-war</artifactId>
<contextRoot>/${app.web.context}</contextRoot>
<unpack>${app.unpack.modules}</unpack>
<bundleFileName>${app.context}-war.war</bundleFileName>
</webModule>
<ejbModule>
<groupId>${project.groupId}</groupId>
<artifactId>${app.context}-bootstrap</artifactId>
<excluded>${exclude.bootstrap}</excluded>
<bundleFileName>${app.context}-bootstrap.jar</bundleFileName>
<unpack>${app.unpack.modules}</unpack>
</ejbModule>
<ejbModule>
<groupId>${project.groupId}</groupId>
<artifactId>${app.context}-ejb</artifactId>
<bundleFileName>${app.context}-ejb.jar</bundleFileName>
<unpack>${app.unpack.modules}</unpack>
</ejbModule>
<ejbModule>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<files>
<file>${basedir}/../src/main/filters/filter-${env}.properties</file>
</files>
</configuration>
</execution>
<execution>
<!-- Properties needed by unexplode profile -->
<id>pre-clean</id>
<phase>pre-clean</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/../src/main/filters/filter-${env}.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- <outputDirectory>${env.JBOSS_HOME}/server/default/deploy</outputDirectory> -->
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>${ds.xml}</include>
</includes>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>copy-app-ds-xml</id>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
<!-- <execution> <id>copy-mysql-driver</id> <phase>install</phase> <goals>
<goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${env.JBOSS_HOME}/server/default/lib</outputDirectory>
<resources> <resource> <directory>${settings.localRepository}/mysql/mysql-connector-java/5.1.6</directory>
<includes> <include>mysql-connector-java-5.1.6.jar</include> </includes>
</resource> </resources> </configuration> </execution> -->
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
Your filter properties rely on the ${env} parameter:
<filter>../src/main/filters/filter-${env}.properties</filter>
This parameter has no value in your "explode" profile. So Maven must be looking for:
../src/main/filters/filter-.properties
I suspect you were hoping it would be filter.properties and not filter-.properties.

Adding java source (.java files) to test jar in Maven

I'm making use of my pom.xml and am was able to generate the jar for src/main/java (say app.jar) as well as for src/test/java (say app-test.jar). I was also able to include my java sources as part of the app.jar (i.e. have both my .class as well as my .java files in the jar).
However for my app-test.jar, i'm not able to include my .java files in it.
This is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Any help would be appreciated.
Thanks.
Update on post on Whaley's suggestion:
Tried the maven-antrun-plugin, but rt now after running mvn package all i'm getting inside my tests.jar is the META-INF folder. .java and .class are not getting included:
This is the part of the pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.testOutputDirectory}">
<fileset dir="${project.build.testSourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Thanks.
I think you probably need to generate a custom assembly using the assembly plugin:
http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html
But a much cleaner solution would be to package test sources separately using the source plugin
http://maven.apache.org/plugins/maven-source-plugin/test-jar-mojo.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-test-sources</id>
<goals>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
you can then have both the test source jar and the test class jar on your classpath and do something like this:
YourClassName.class.getResource("YourClassName.java");
or more generically:
public static InputStream getSourceForClass(final Class<?> clazz) {
final String baseName = clazz.getSimpleName();
return clazz.getResourceAsStream(baseName + ".java");
}
to access the sources
The includes property of the jar plugin only allows the inclusion of files from a relative path from the plugin's classesDirectory property. So that won't work unless you copied your .java files to ${project.build.outputDirectory} somehow. You could do something like that with the maven-antrun-plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.outputDirectory}">
<fileset dir="${project.build.SourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources