Specify non-compile dependencies in Maven and package them as resources - java

I need to build a jar file that includes (other, external projects') Maven artefacts.
The artefacts are to be included just like stuff in src/main/resources, without any processing. Even though they happen to be jar files themselves, they are not compile time dependencies for my code and should not be added to the classpath, at neither the compile, the test, or the runtime stages.
I can get this done by downloading the files and placing them into src/main/resources, but I would rather have them resolved using the Maven repository.

Here's an example of what you can add to your pom-- it'll copy the artifact with the specified ID from the specified project into the location you specify.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>id.of.the.project.group.to.include</groupId>
<artifactId>id-of-the-project's-artifact-to-include</artifactId>
<version>${pom.version}</version>
</artifactItem>
</artifactItems>
<includeArtifactIds>id-of-the-project's-artifact-to-include</includeArtifactIds>
<outputDirectory>${project.build.directory}/etc-whatever-you-want-to-store-the-dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

You could use the dependency plugin to download and put your required artefacts into the target/classes directory during the process-resources phase.
See the example usage for copying artefacts

Since you say you want to end up with a jar, the assembly plugin with a custom assembly descriptor would probably solve this.
Add a <dependencySet> and specify the <unpack> option to ensure those external artifacts get flattened out inside your jar.

Related

Create a jar artifact with minimal pom / parent pom.xml / flattened / regenerated pom [maven]

I have a multimodule maven project and distribute the resulting jar file to different parties. Part of the jar file is the pom.xml file (in META-INF/maven/.../pom.xml).
The problem with that is, that the parent pom.xml is missing which contains a complete list of the dependencies and the necessary dependency-versions etc.
So I tried several things:
Solution 1
I added the effective pom to the jar file
Problem
the pom file is way too big, with too much information (partly internal, local etc)
Solution 2
I combined two plugins and managed to additionally add the parent pom.xml file to the jar.
Problem
This is way better than S1 however the parent pom again contains a (grand)parent and also tags like <scm> which are internal and could & should not be handed to the outside world
Now I wanted to start to manipulate the parent pom and remove some parts etc. However there must be a better solution and others who have the same problem?
What I need is (e.g) a plugin which creates a clean "releasable" pom.xml file with only the dependencies (and of course artifact, groupid, version) and can then be imported by external parties into their repo without any conflicts. Is that possible?
The only thing remotely related is the eclipse tycho pom generator plugin. It is however eclipse specific...
The flatten-maven-plugin is exactly what I needed! Thanks to khmarbaise
I use the following configuration and the pom is looking beautiful :-)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<pomElements>
<repositories>flatten</repositories>
</pomElements>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>

Copy resource files from submodule into main module in maven

In my java application, I am just wondering if it is possible to copy or just use resource files from under src/main/resources from a maven submodule into a main maven module (which has the submodule as a dependency) src/main/resources folder, so I don't need to copy manually all the resource files again into my workspace.
You can use the maven dependency plugin to do that.
Here is a configuration that you can include in your main module pom.xml in the build > plugins section.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>initialize</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>hepl.cecotepe.epick</groupId>
<artifactId>wsdl</artifactId>
<version>${wsdl.version}</version>
<type>jar</type>
</artifactItem>
</artifactItems>
<overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources/webservices</outputDirectory>
<includes>**/*.wsdl, **/*.xsd</includes>
<excludes>**/META*/**, **/Documentation/**</excludes>
</configuration>
</execution>
</executions>
</plugin>
The artifactItem is the module from which you want to copy the files into your module. In this case, you will fill the artifactItem fields with your submodule groupId, artifactId and version.
The plugin will take all .wsdl and .xsd files across all folders (including target, src and subfolders) except the Documentation and META* folders (and subfolders). The files will be copied each time in the main module /resources/webservices folder.
To make the whole thing work, you need to package the submodule as a jar (include jar in your submodule pom.xml) and install it in your local maven repository using mvn install. After, you can copy the files in the main module with mvn initialize
NOTE : The initialize phase is included in the default lifecycle and is the first phase executed. When you use the default lifecycle, the first thing maven will do will be to copy the files from the submodule to the main module.

Maven package in-project repository dependencies inside jar

I am using the in-project repository dependency solution to include a third party jar as a dependency in my Maven project. I'm following the instructions on this blog for that.
Now, I want that when I package my Maven project into a jar, the jar that is created should have a lib folder with the third party jar in it. However, I do NOT want the other dependencies to be packaged in the jar. (I don't want a fat jar with ALL dependencies packaged inside it, I just want a jar with the one third party dependency jar packaged inside it).
I have been trying to play around with the maven-dependency-plugin and the maven-jar-plugin, but I've not been able to achieve what I want.
Can someone please help me out?
You can use maven-dependency-plugin (look here) as shown below, this plugin will provide lots of options to include jars which ArtifactId (i.e., <includeArtifactIds>) or groupId (<includeGroupIds>), etc...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/
classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeArtifactIds>YOUR_THIRDPARTY_JAR_NAME</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
So, the above code will add YOUR_THIRDPARTY_JAR_NAME.jar into your final .jar file's lib folder.
Take a look at Maven Assembly Plugin. It allows filtering included dependencie, so you can choose what deps to include in the assembly

Copying Maven Dependency into Project Structure

I've been stuck on one specific Maven task for a while now, and I haven't been able to find anything specific to it.
I'm trying to host a few files as a dependency in my internal Maven repository (Sonatype Nexus). The catch is, I need it to be placed into a specific folder in the project structure.
In other words, I need dependency A to be placed into /src/WebContent/VAADIN/themes/${project.name}/
Looking over this helpful page from the Maven website I was able to copy the dependency onto the hard disk in the exact location that I wanted, but it's not actually part of the project itself, so other classes have no idea it's there. Here's the exact code, though it's mostly a copy from the linked page:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.dsa.mobi</groupId>
<artifactId>DSAStyles</artifactId>
<version>1.0.0</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.sourceDirectory}/main/webapp/VAADIN/themes/${project.name}/</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Any help would be greatly appreciated, even if it's just a link to some documentation that might clear this up -- I looked over all of the XML documentation for Maven, which is confusing to find and more confusing to navigate, but I have yet to find a panacea for this >.>
Thanks in advance!
EDIT:
Here's an image of the folder structure in Eclipse's Project Explorer:
As you can see, only addons.scss and fisafetyapp.scss show up in this folder, whereas I'm trying to get Maven/the dependency to plug files into this exact location.
On disk, the dependency is being placed in this folder correctly, ie C:/myuser/fisafetyapp/src/main/webcontent/vaadin/themes/fisafetyapp/. However, it's not showing up in the Project Explorer, which keeps the other files from seeing it as the location is highly specific.
Your statement is right, not forget in your pom declaration to dependency:
<dependency>
<groupId>com.dsa.mobi</groupId>
<artifactId>DSAStyles</artifactId>
<version>1.0.0</version>
</dependency>
Copy the dependency with the maven-dependency-plugin into the src/main/resources folder in generate-resources phase (don't forget to add these files to source control ignore list). Then the resources plugin will automatically copy it into the target/classes folder and it will be also automatically added to your end artifact.

Does maven have an ability to pack single *.dll to jar without any sources?

I'd like to add *.dlls as third party libs to my repository and during packaging process just pack them to *.jar, sign them and copy to some specific folder.
Signing and coping are well done and work correctly (as expected by using maven-dependency-plugin and maven-jarsigner-plugin). But I didn't find any method to automatically pack single dll to jar (without any sources like maven-assembly-plugin does).
Solution that I see by the time: add to my repository not a "pure" dll, but already packed to jar lib (packed by myself)... but it's not a good idea, I guess)
It sounds like you've successfully retrieved your .dll (with dependency plugin) and signed it (jarsigner plugin), and it's somewhere in your ${project.build.directory} (which defaults to target).
If that's correct, give this a try:
Define the packaging of your project as jar
Retrieve dlls
Make sure the jarsigner:sign goal is bound to the prepare-package phase. It binds to package by default and we need to ensure jarsigner:sign runs before jar:jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>sign</id>
<phase>prepare-package</phase> <!-- important -->
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
Configure the jar plugin to include the signed dll(s)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<!-- using this ID merges this config with default -->
<!-- So it should not be necessary to specify phase or goals -->
<!-- Change classes directory because it will look in target/classes
by default and that probably isn't where your dlls are. If
the dlls are in target then directoryContainingSignedDlls is
simply ${project.build.directory}. -->
<id>default-jar</id>
<configuration>
<classesDirectory>directoryContainingSignedDlls</classesDirectory>
<includes>
<include>**/*.dll</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Now, running mvn clean package should give you a jar containing your signed dlls.
If JACOB requires manifest config there are docs explaining how to do this.
Good luck!
I would recommend to pack your dll's as a zip archive via maven-assembly-plugin and let that module deploy the zip archive as attached to your usual pom. The packaging of that project should be pom instead of default.
I would be a little bit confused if i download a jar and find dll's inside it,
but if you prefer you could create jar via the maven-assembly-plugin or use the maven-jar-plugin.

Categories