Use JVM Options in Maven Plugin to get rid of relative path dependency - maven

I've got a project A which consists of several modules. This is the "product" itself (server/client application). Furthermore there is another project B which is a specific customer project.
When the client+server *.jar of project A are assembled, resources of project B need to be integrated. I.e. copied into a resource folder.
I'm using the maven-resources-plugin to copy some of the property files:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>../../commonDashboard/src/main/resources/</directory>
<includes>
<include>commonDashboard.*.properties</include>
<include>db.*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This approach requires a relative dependency between both projects. I want to get rid of the relative path and use a path definition which is provided as VM Options in IntelliJ. So when running the assembly modules I need to access the VM options. I'm not quite sure if this is possible at all. I guess it's not, right? Do you know any other approach to get rid of this dependency between both projects? I don't want to add the options into the maven assembly file itself.

Related

How do I add a file to the artifact (jar or war) created by maven?

I have an antrun task (goal run on phase prepare-package) set up that creates a file and saves it in /target/foo.bar. How do I add that to the artifact that gets created by maven (depending on module, it could be a jar or a war file)?
I have tried it with resources, with the builder-helper plugin, and the jar plugin - no luck:
<resources>
<resource>
<directory>target</directory>
<includes>
<include>**/foo.bar</include>
</includes>
</resource>
</resources>
That doesn't seem to do anything.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/foo.bar</file>
<type>bar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
According to debug output, this seems to install something extra in the repo, but doesn't add foo.bar to the artefact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>../foo.bar</include>
</includes>
</configuration>
</plugin>
No observable effect, either. :(
(I assume this might work for the war file, but I'd prefer one way to do it for both artifact types, if possible - plus I really need this to work for the jars, too...)
Is there a way to do what I want to do?
(I'd prefer not saving my file in src/main/resources first; I thought the packaging processes would pick files up from the build directory after they were placed there anyway, but I suppose I misunderstood something...)
I had my file saved to project.build.directory. Changing that to project.build.outputDirectory means the jar plugin is picking it up, without the need of any other plugins. Unfortunately, that doesn't sort the war issue out... :(
I had a similar problem and stored the resulting artifact in
target/${project.name}-${project.version}/foo.war
To be more precise, I created the war from a directory using the ant target
<war warfile="target/${project.name}-${project.version}/foo.war" basedir="somedir" />
Then it was correctly added to the surrounding ear.

Java EE / Maven - package WEB-INF folder into multiple wars

I have a Java EE project packaged as an EAR using Maven, it contains two WAR modules (one web one mobile) in following structure:
EAR
-Entities.jar
-EJB.jar
-Test.jar
-Web.war
-Mobile.war
I have a directory within the Web.war/WEB-INF which contains some .xhtml files that are common to both .wars
Is there any way to instruct Maven to copy this directory into the other .war automatically during build?
eg. I would like..
WebApp-Web/src/main/webapp/WEB-INF/emails/*
to build into
WebApp/WebApp-web/WEB-INF/emails
AND
WebApp/WebApp-mobile/WEB-INF/emails
(I am using the maven-war-plugin btw)
If you add your shared resources into the "parent project" (or elsewhere), you could define the relative path in maven-war-plugin's webResources tag (for both web projects):
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>../src/main/resources</directory>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
Another way would be to use the maven-resources-plugin to copy the files from a relative path at build-time, but I believe the maven-war-plugin approach is better.
Using maven-resources-plugin: (source how-to-get-a-war-package-with-resources-copied-in-web-inf)
I only changed the directory, to make it relative to its parent (ie: ../src/main/resource)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>../src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
If you're using a web container that implements Servlet 3.0 or newer then you can make use of the web-fragment feature to build these common files into a jar file that is added to the WEB-INF/lib directory of each web application.
Make web resources such as your xhtml files available by placing them in the META-INF/resources directory of the jar file. Any web.xml configuration that might be associated with these xhtml files can be placed in a META-INF/web-fragment.xml file.

Configure maven-shade-plugin to include src/main/webapp

I'm working with a simple project, with the webapp directory in the standard location (src/main/webapp). For some reason, it looks like the maven-shade-plugin doesn't include that in the final generated jar - it will only include artifacts src/main/resources by default.
I have tried to use the following configuration, with no luck:
<configuration>
<artifactSet>
<includes>
<include>*</include>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
Any suggestions on how to do that without having to move src/main/webapp into src/main/resources/webapp?
Thank you.
Eugen.
Have you tried to update your build section with your resource path ?
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
EDIT
As already said, shade purpose is not to war packages.
You need to put it in src/main/resources/webapp or specify the appropriate location explicitly for it to be caught up properly. Annoying, but it works.
For those asking why this kind of thing makes sense to do, it's a typical setup for a jar with an embedded web server like Jetty.
I use the resource plugin to move the webapp to the classes folder.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/webapp</outputDirectory>
<resources>
<resource>
<directory>src/webapp/dist</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This way I can place the folder with my webapp anywhere, define the destination directory and it is included in my shaded jar.
It sounds you would like to package sources into a package. There exists a maven-source-plugin which will create a jar which contain the source files. Furthermore this is usually done during a release cycle. As already mentioned the maven-share-plugin intention is to package jar files together (their contents; keyword: ueberjar).
I would use the war plugin http://maven.apache.org/plugins/maven-war-plugin/faq.html#attached to create a jar of a webapp project. You have already set <packaging>war</packaging>?
What's the point in adding your webapp resources into a JAR? That's WAR-like.
Change your packaking type to war and it will work.

How to include external source files in maven-jar-plugin

We are building a jar file from external(to the project) classes.
That works fine but we have not been able to figure out how to also include the external source files. I have tried using the "< includes >" tag but only end up with a manifest file in the final jar when used. I have looked at using the maven-resources-plugin but either I used it wrong or it doesn't work in my case. Here is a copy of our of code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<!-- <phase>generate-resources</phase> -->
<phase>clean</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${itendant.path}/web/rocket/WEB-INF/classes</classesDirectory>
<finalName>${itendant.jar.name}</finalName>
<outputDirectory>${itendant.jar.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Sources? Do you mean external dependencies? These should also be managed with maven, using mvn deploy:deploy-file as described on http://maven.apache.org/plugins/maven-deploy-plugin/usage.html , and imported in your pom.xml.
If you really mean external resources, then a proper resources declaration would be:
<project>
...
<build>
...
<resources>
<resource>
<directory> [your folder here] </directory>
</resource>
</resources>
...
</build>
...
</project>
You can have multiple tags if you have multiple resource directories, of course. Also note that building anything during clean is questionable, as clean is not run every build - package would be a better option.

how to assemble external artifacts into one global directory with the maven-assembly-plugin (or otherwise)

I am experimenting with Maven and I am trying to mavenize a project originally build with shell scripts.
With the Maven rule-of-thumb: one project, one artifact, I created the following structure:
<PROJECT>
<MODULE-1>
<MODULE-2>
<MODULE-3>
..
<MODULE-N>
<RESOURCES>
<DISTRIB>
The RESOURCES module is structured as follows:
<RESOURCES>/src/main/resources/<MODULE-1>/bin
<RESOURCES>/src/main/resources/<MODULE-1>/lib
<RESOURCES>/src/main/resources/<MODULE-1>/doc
<RESOURCES>/src/main/resources/<MODULE-2>/bin
<RESOURCES>/src/main/resources/<MODULE-2>/lib
<RESOURCES>/src/main/resources/<MODULE-2>/doc
...
<RESOURCES>/src/main/resources/<MODULE-N>/bin
<RESOURCES>/src/main/resources/<MODULE-N>/lib
<RESOURCES>/src/main/resources/<MODULE-N>/doc
The reason for doing it this way was that the resources above are needed at runtime, not compile-time and they are mostly property files, config files and shell scripts to invoke the various jar-files. For the final resources step, I wanted to combine the subdirectories into one global bin/lib/doc directory. However, I do not see an option in the assembly descriptor to strip of the prefix of the modules to get to what I want:
<RESOURCES>/target/resources/bin
<RESOURCES>/target/resources/lib
<RESOURCES>/target/resources/doc
where bin would contain all the files found in the /src/main/resources//bin directory, /src/main/resources//bin directory etc. Similarly for lib, doc.
My question: should I have a:
<MODULE-1>/src/external/resources/bin
<MODULE-1>/src/external/resources/lib
<MODULE-1>/src/external/resources/doc
...
<MODULE-N>/src/external/resources/bin
<MODULE-N>/src/external/resources/lib
<MODULE-N>/src/external/resources/doc
structure instead, that I would then access through a dependency-set? These files should not be part of the jar-file of the various modules and therefore cannot be stored in src/main/resources of their respective projects. Or is what I want to achieve doable by using some other maven plugin instead?
In the DISTRIB module I would combine the output of the RESOURCES module with the JAR-files and dependencies to create a directory structure that would then be used with a packaging tool (Solaris package and WiX installer).
Any help would be appreciated!
In the pom fort he resources module set the source directory to something other than /src/main/resources/ so that it does not copy them to the target folder. Then use the Maven Resources Plugin to copy the resource files to target/bin and target lib etc.
e.g.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/bin/</outputDirectory>
<resources>
<resource>
<directory>src/external/resources/bin</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/lib/</outputDirectory>
<resources>
<resource>
<directory>src/external/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>`

Resources