I'm new to Maven and I'm trying to configure Maven to generate 2 jars: one for development and one for production. The only difference between them is a config.properties file that have the database connection different so I thought I could use Maven profiles.
To my surprise I can't generate both files at once. When using profiles, each time you build you have to select the profile and a jar (in my case) will be created using the profile. The thing is that it will create 2 exactly equals jars, one without a classifier and one with the classifier (like myjar.jar and myjar-prod.jar) so if I want to generate the dev and the prod jar I have to create 4 jars (running first Maven with one profile and after that with another profile)
Why is this? Doesn't make any sense to me... but ok...
My question is:
Is there a way I could avoid the two jars from being generated? I mean, I want to have different profiles, and I have accepted (with grief) to execute multiple times the build process (one for each profile), could I avoid to have each time 2 jars and have only one without the classifier?
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.p2p.</groupId>
<artifactId>LoadACHFiles</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyProject</name>
<url>http://maven.apache.org</url>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config-*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
<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>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.0</version>
<type>jar</type>
</dependency>
</dependencies>
<profiles>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
If you are okay with having classified jars, you may do what you want without profiles, so you may have jars for all environments with a single build command. The key is to understand how Maven filtering works.
This is expanding on an answer I provided to a similar question. Start with that setup. Then:
Create config.properties in your src/main/resources, containing properties your app needs.
my.database.url=${database.url}
my.database.user=${database.user}
my.database.pw=${database.pw}
Now, create prod.properties and dev.properties in ${basedir}/src/main/filters holding appropriate values for each environment.
database.url=URL-for-dev
database.user=user-for-dev
database.pw=pw-for-dev
When you run mvn clean package, Maven will copy the contents of /src/main/resources, including config.properties, doing property replacement during the copy. Because there are multiple executions of both resources and jar plugins, Maven will create separate classified jar files. Each will contain a config.properties file, holding the correct properties for the environment. The filters will not end up in the built jars.
I made it removing the maven jar plugin in the profile section. Changed this:
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
For this:
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Related
I am creating a JAR using maven plugin. But all the files places inside the src/main/resources folder are being directly placed inside the JAR, rather than the resources folder. (resources folder is not being created inside JAR at all)
Due to which the application which is using JAR is not able to find log4j2.xml file. Earlier resources folder was being created, but now somehow it's not being created.
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>testgroup</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<aspectj.version>1.8.10</aspectj.version>
<java.version>1.8</java.version>
<codehaus.mojo.version>1.11</codehaus.mojo.version>
<log4j.version>2.11.1</log4j.version>
<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
<maven.assembly.plugin.version>2.4.1</maven.assembly.plugin.version>
<maven.resources.plugin.version>2.6</maven.resources.plugin.version>
<sonar.maven.plugin.version>3.3.0.603</sonar.maven.plugin.version>
<artifactory.maven.plugin.version>2.6.1</artifactory.maven.plugin.version>
<eclipse.lifecycle.mapping.version>1.0.0</eclipse.lifecycle.mapping.version>
<junit.platform.surefire.provider.version>1.0.1</junit.platform.surefire.provider.version>
<maven.surefire.plugin.version>2.19.1</maven.surefire.plugin.version>
<codehaus.mojo.maven.plugin.version>1.6.0</codehaus.mojo.maven.plugin.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>package.Test</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.maven.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jfrog.buildinfo</groupId>
<artifactId>artifactory-maven-plugin</artifactId>
<version>${artifactory.maven.plugin.version}</version>
<inherited>false</inherited>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${codehaus.mojo.version}</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${codehaus.mojo.maven.plugin.version}</version>
<configuration>
<mainClass>package.Test</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.surefire.provider.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<!-- This is added to ignore error in life cycle of aspectj-maven-plugin
due to bug in m2eclipse -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>${eclipse.lifecycle.mapping.version}</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${codehaus.mojo.version}</version>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</project>
Project Structure:
Project
-----src/main/java
-----src/main/resources
log4j2.xml
-----src/test/java
Jar Structure :
Jar
-----log4j2.xml
In the application console I ma getting message:
ERROR StatusLogger Log4j2 could not find a logging implementation.
Please add log4j-core to the classpath. Using SimpleLogger to log to
the console...
I've had issues with the log4j2.xml files several times myself. First of all: maven is doing the right thing, by placing that XML file into the root of the JAR - which is exactly, where it belongs (link to docs - look at "Automatic Configuration" point 9).
Now, as Log4j2 does apparently not find the logging configuration, some things are going wrong.
These could be:
there is a syntax error in your config file
your logging file makes declarations, that Log4J is not able to resolve (e.g. using a specific logger that can't be found on the classpath)
one or more of your dependencies also have a similarly named log4j2.xml file at the root of your dependencie's jar file. In that case, there is an ambiguity, that Log4J can't resolve at runtime
when you run your application, you've explicitly specified where log4j should be looking for that file.
in some cases, you might get a hint from log4j about what has happened. Anyways, I would suggest, you start your application with a -Dlog4j2.debug=true parameter. That will cause Log4J to print out internal debugging information which will give you some more hints about what is going on. (If you don't know how to use -D params, be sure to look it up, as their positioning can be a bit tricky. If not put at the right place of the script execution, they'll get ignored)
I hope, that helps you to specify the problem. If not, it would be super helpful, to add the console output of your application to the question. Ideally with debug output on.
Try specifying the output folder explicitly like:
<resources>
<resource>
<targetPath>resources</targetPath>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src</directory>
</resource>
</resources>
Hope this helps.
Cheers!
I have a project like this:
TestArt
-----------index.jsp
-----------pom.xml
-----------src
--------------main
------------------java/com/web/LoginServlet.java
------------------webapp/WEB-INF/classes/com/web/LoginServlet.class
-----------target
-----------------testproject-1.0-SNAPSHOT.war
-----------------testproject-1.0-SNAPSHOT.war.original
-----------------testproject-1.0-SNAPSHOT
----------------------------------META-INF
----------------------------------WEB-INF
---------------------------------------classes/com/web/LoginServlet.class
---------------------------------------lib (here are all the jars)
and pom.xml like this:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.util</groupId>
<artifactId>testproject</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test Project</name>
<url>https://test.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
...
</dependencies>
<properties>
<java.version>1.7</java.version>
<start-class>com.web.LoginServlet</start-class>
</properties>
<build>
<outputDirectory>${basedir}\src\main\webapp\WEB-INF\classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>8.0.30.2</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>install</defaultGoal>
</build>
</project>
After I run mvn package it creates META-INF and WEB-INF folders inside root/target folder and also places .war files in there.
I want maven to also copy a WEB-INF folder from target into the root (TestArt) once the packaging is done. Is there any way to achieve it?
I really would recommend to spend time on fixing the original problem, but if you want:
Use Maven Antrun Plugin to copy a folder from one place to another, as described here:
https://stackoverflow.com/a/694175/927493
The details of the copy command in Ant are explained here:
https://ant.apache.org/manual/Tasks/copy.html
Couldn't figure it out with Ant, so I used this answer. In my case it would look like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>target/testproject-1.0-SNAPSHOT/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
EDIT: This plugin copy pastes files one by one, instead of taking a folder and copy pasting it with all it files once. This leads to broken links between classes and directories and nothing seems to work. For now, I'm using the batch script:
xcopy "C:\...\Tomcat 8.5\webapps\TestArt\target\testproject-1.0-SNAPSHOT\WEB-INF" "C:\...\Tomcat 8.5\webapps\TestArt\WEB-INF\" /E /Y
Using this answer's Maven plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>sign</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<exec executable="${basedir}\copy.bat">
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
I managed to achieve a desired result.
But if there are solutions in pure Maven that would copy a directory all at once, I'd love to hear them.
We are using maven profile to build a jar specific to tomcat.
<profile>
<id>TOMCAT</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-tomcat-${project.version}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-j2ee-management_1.1_spec</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.javaee</groupId>
<artifactId>jboss-jms-api</artifactId>
<version>1.1.0.GA</version>
</dependency>
</dependencies>
</profile>
My expectation is to create a single jar with name acme-tomcat-0.0.1-SNAPSHOT.jar but on building the project, maven is generating another one (a default one) acme-0.0.1-SNAPSHOT.jar. How can we avoid the generation of the second one (acme-0.0.1-SNAPSHOT.jar)?
Thanks
I've only used Maven a few times to build projects, and I've generated WARs for Tomcat instead of JARs, but the process should be similar.
My build looks like this:
<build>
<finalName>sb</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
The header at the top goes like this:
<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>org.majisto.socialbusiness</groupId>
<artifactId>socialbusiness</artifactId>
<version>0.0.2</version>
<packaging>war</packaging>
<name>socialbusiness</name>
I imagine if you switch the packaging row to JAR you should get one JAR file out of it. The finalName in the build determines the filename and I think where you have it might be the confusing part to Maven. Let me know if you have any other questions. Maven made working with Spring so much easier. Are you using "maven package" in the CLI?
By changing the build section in the profile section from
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-tomcat-${project.version}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
to
<build>
<finalName>${project.artifactId}-tomcat-${project.version}</finalName>
</build>
helped me to solve this issue
I'm trying to build a Maven parent project script which incorporates a 3rd party project (with all its dependencies) and another couple of projects of mine.
I'd like to copy all dependencies (I mean ALL dependencies, including jars requested by other dependencies which in turn are requested by one of my child project) to a parent project directory, e.g. "lib", so that I can just run java with a wildcard classpath:
java -cp "lib/*" package.blah.blah.Main
I tried with various methods using the maven-dependency-plugin such as copy-dependencies, but all I can point Maven to is my own child projects, not their dependencies (I mean in the parent project pom).
This is the parent project pom I've been writing:
<?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">
<properties>
<server.version>7.0.2</server.version>
<ext.version>1.0-alpha3-SNAPSHOT</ext.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>org.kontalk</groupId>
<artifactId>tigase-kontalk</artifactId>
<version>devel</version>
<packaging>pom</packaging>
<modules>
<module>../tigase-server/modules/master</module>
<module>../tigase-extension</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.kontalk</groupId>
<artifactId>tigase-server</artifactId>
<version>${server.version}</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>org.kontalk</groupId>
<artifactId>tigase-extension</artifactId>
<version>${ext.version}</version>
<type>jar</type>
</artifactItem>
</artifactItems>
<outputDirectory>bin</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>tigase-server</artifactId>
<version>${server.version}</version>
</dependency>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>tigase-extension</artifactId>
<version>${ext.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
It will copy just the two direct child project jars in the bin directory, but nothing else.
I'm thinking of doing a manual copy (as in a Maven copy) to brutally copy all jars from my child projects' target/dependency directories, but it just seems... brutal.
If it can't be done with the existing Maven software, I can even be content with using maven exec directly (which, I hope, should set up classpath automatically, right?)
EDIT: I'd like to modify the child projects poms as little as possible, especially the 3rd party one.
If you want to build a simply runnable jar file with maven then you can use this config:
<profiles>
<profile>
<id>dist</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>yourJarName</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>your.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
then you can build it using mvn clean package -Pdist
edit: then you can run it with java -jar target/yourJarName.jar
I ended up using copy-dependencies. This is the main project pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/jars</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>jars</outputDirectory>
<resources>
<resource>
<directory>../tigase-extension/target/lib</directory>
<excludes>
<exclude>junit*</exclude>
</excludes>
</resource>
<resource>
<directory>../tigase-server/target/lib</directory>
<excludes>
<exclude>junit*</exclude>
</excludes>
</resource>
<resource>
<directory>../tigase-extension/target</directory>
<includes>
<include>tigase-extension-*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In the submodules I've added to the same plugin this code:
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
Basically, the submodules copy their dependency jars into target/lib, then the main project copies those jars into its own jars folder.
It has some project-specific filenames and other hard-coded things, but still I think this is the best option if I want to use only Maven-provided tools. Probably a new Maven plugin would have been optimal, but I don't really have the time.
I'm now programming Java for a year and had to take over several projects from a previous programmer. I'm not a programming newby since I started with .NET programming in 1998. Now for a Java project, I need to support 24 versions (and rising) of a base library (all the same structure but they are specific for a version of the 3th party application).
I started with Maven two days ago and tried this on a small demo workspace in order to learn Maven and get the required things working.
Each supported version has several jars which I encapsulated in a user library.
To make a build, I would need to modify this library to the correct version and do an export of the project as a Runnable-Jar (everything include in the jar due to license requirements).
So I created my first POM and here it is:
<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>PPC</groupId>
<artifactId>PPC</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>PPC</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>LibraryProject</groupId>
<artifactId>LibraryProject</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies> <profiles>
<profile>
<id>PPC_8.1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<finalName>PPC_8.1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<inherited>true</inherited>
<version>2.10</version>
<configuration>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>org.eclipse.jdt.USER_LIBRARY/Primavera-P6-8.1</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>com.company.main.App</mainClass>
<onejarVersion>0.97</onejarVersion>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>PPC_7.0</id>
<build>
<finalName>PPC_7.0</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<inherited>true</inherited>
<configuration>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>org.eclipse.jdt.USER_LIBRARY/Primavera-P6-7.0</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>com.company.main.App</mainClass>
<onejarVersion>0.97</onejarVersion>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
</project>
As you can see, I have two profiles. One for v8.1 and one for v7.0.
My 'build' flow:
I run 'mvn install' on the libraryProject first.
Then I run 'mvn package' on the main project. This one build v8.1 (Default profile) as a runnable jar with all depencies in it except the user library.
The next call is 'mvn package -PPPC_7.0' which builds v7.0 without the user library.
So my question: What can I change/doing wrong so the user library is added to the runnable jar for each profile?
Thanks in advance,
Jo
PS: I'm using Eclipse Luna (Java EE IDE)
What I use is:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.clouway.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
`
Good luck!
Robby Cornelissen commented a suggestion a while back:
Any chance of just packaging that up and using it as a dependency? That would definitely solve your problem... – Robby Cornelissen Jun 11 at 8:52
I didn't want to do that, but I did not receive new suggestions (and did not find a solution myself).
So I packaged them as suggested and added them as dependency.
This is working for me now.
Thanks and until next time
Jo