Deploy as ROOT and SpringLoaded Hippo CMS - maven

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>

Related

maven override/replace file into WEB-INF/classes

I want to override/replace my spring config xml file with specified location file while package as war.
And I do not want to use filter plugin (filter plugin must to use dolloar placeholder, it will run with error while local deploy), is there any plugin or setting I can use to do this?
project structure as follow:
ROOT
----config
----prd
----spring-servlet.xml
----web.xml
----src
----main
----java
----resources
----spring-servlet.xml
----webapp
....
My pom.xml is like as follow:
...
<profiles>
<profile>
<id>prd</id>
<properties>
<filterDir>prd</filterDir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>${basedir}/config/${filterDir}/web.xml</webXml>
<webResources>
<resource>
<directory>config/${filterDir}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>mvc-dispatcher-servlet.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
run maven use :mvn clean package -P prd.
Will work fine if not set tartgetPath, file copied to web root.
You can use copy-resources of maven to do this.
Example maven code should be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>your-target-directory</outputDirectory>
<resources>
<resource>
<directory>source-directory</directory>
<!--You can also mention files too-->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Read more about copy-resources for correct implementation.

How are non-conflicting configurations resolved if multiple profiles are activated?

Following up this question:
I can't find information on how the union of multiple (active) maven profiles is built, under the condition that they are not conflicting.
Consider the following example:
I want to control how my test cases are run with maven-surefire-plugin. First I need to configure two different environments (jenkins-CI-server) and local. Second I want to enable running different sets of tests.
I would have four profiles:
env_jenkins for running on jenkins
env_local for running on local
testset_A for running some tests
testset_B for running some other tests
Note that for all those profiles I need to define the plugin configuration for the maven-surefire-plugin.
(See below for the concrete xml-configuration)
Then I would like to combine the profiles to run - for example - testset A on jenkins.
My questions:
Is such a behaviour supported by maven?
On which stage does the overriding mentioned in the related question occur? Is the entire plugin configuration overridden (-> my example would not work). Or only really conflicting parts (-> my example would work)?
Does the overriding behaviour depend on the plugin or is it consistent among all maven plugins?
Example pom:
<profiles>
<!-- handle system configurations (e.g. one for jenkins environment, one for local) -->
<profile>
<id>env_jenkins</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed</arquillian.launch>
<jbossHttpPortOverride>8080</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>env_local</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- define different test sets -->
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testset_B</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>TestB1.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
It works using the same inheritance/merging process that works between parent and children POMs. Using the Maven help plugin can confirm this (using Maven 3.3.9):
Order of profile declarations in the POM matters. A plugin configuration inherits the same plugin's configuration declared in an active profile appearing before it in the POM.
The attributes combine.self and combine.children can be used to control the merging.
Example with default settings (without using combine.self or combine.children, i.e. with combine.children="merge" implicitly):
mvn help:effective-pom -Penv_local,testset_A
Effective POM shows merged configurations:
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
...
Example with combine.self="override":
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
mvn help:effective-pom -Penv_local,testset_A
Effective POM shows only testset_A's configuration:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
Example with combine.children="append":
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.children="append">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
mvn help:effective-pom -Penv_local,testset_A
Effective POM shows merged configurations. In this case, it happens to be the same as the first example. However, had testset_A introduced configuration XML elements that already exist in env_local, they would have been appended:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration combine.children="append">
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration combine.children="append">
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>

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

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.

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.

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