Maven Assembly Plugin jar-with-dependencies -> No Dependencies in Jar - maven

The following reference mentions the descriptor Reference jar-with-dependencies. Afaik it is a predefined assembly, which includes all jar dependencies into a single big self-contained jar file. This is great if you have multiple dependencies and need to copy your project to another machine because you don't need to update/delete obsolete libraries separately.
https://newfivefour.com/category_maven-assembly.html
I added the maven-assembly-plugin to my pom, and the MyTool.jar-with-dependency.jar is created. I expected that the jar contains all external dependencies, but it is the same as the normal MyTool.jar and does not contain any dependencies like apache.commons or apache.logging.
The important detail is that the dependencies scope is set to provided. Without this it works as expected. But I use the scope later on with the maven-dependency-plugin to copy all dependencies in the provided scope to a specific directory.
[...]
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
<scope>privided</scope>
</dependency>
</dependencies>
<build>
<!--pluginManagement-->
<plugins>
<plugin> <!-- This is the plugin I added. -->
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
I use Apache Maven 2.2.1 (rdebian-14).
How can I include the dependencies from the provided scope? Or is there an other solution?

Related

Simple Java Mail dependencies not in the jar file

I used simplejavamail in my maven project. The program can send out the email if I run it from Intellij IDE. But when I create a jar file, and run it from the jar file, then I got class not found for all the simplejavamail classes. And I open the jar, I find out that they are not included in the jar. But all the other dependency classes are there. Any one have meet this issue before?
parts of my pom.xml
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>6.4.3</version>
</dependency>
<build>
<finalName>my-project-name</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I'm having the same problem. It appears that the dependencies (Ex. Email, Mailer, EmailBuilder, etc) appear in the org.simplejavamail.api repository. I'll update you if I find a working solution with v6.4.3 but I have a feeling we may need to include additional dependencies.
Edit: To at least patch your problem,
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>5.5.1</version>
</dependency>
The 5.5.1 version still has the classes in the jar. You can reference this for yourself here:
https://www.javadoc.io/doc/org.simplejavamail/simple-java-mail/5.5.1/index.html
Then click on the different versions to see what classes exist.
I think something went wrong in their builds since v6.
Let me know if this helps!

Using Maven-bundle-plugin, how can i package dependent jars as .class (extracted jars)

I need maven-bundle-plugin to generate the jar with expanded dependent jars.
My plugin configuration in pom.xml looks like:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>${project.build.outputDirectory}/META-INF/</manifestLocation> <!-- make sure this is present! in the example of maven bundle plugin documentation, this piece is NOT present -->
<exportScr>true</exportScr> <!-- be sure to add this line as well -->
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<_dsannotations>*</_dsannotations>
<!-- we explicitly import the interfaces package, not the implementations, otherwise we get into dependency and version hell -->
<Import-Package>com.ooo.dis.common.extensions.interfaces;version=${platformVersion},com.ooo.dis.analysis.common.interfaces;version=${platformVersion},javax.json,javax.ws.rs.client</Import-Package>
<Build-Timestamp> ${maven.build.timestamp}</Build-Timestamp>
<Include-Resource>{maven-resources},schemes=target/classes/schemes</Include-Resource><!-- override schemes with the one generated by the processor -->
<Embed-Dependency>*</Embed-Dependency>
</instructions>
</configuration>
</plugin>
maven-assembly-plugin works for this. but Is there some way this can be achieved using maven-bundle Plugin?
The bundle plugin has a config option to inline (expand) the classes of dependency jars instead of embedding the jars themselves:
<Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>
This is mentioned at the bottom of the "Embedding dependencies" section in the plugin doc.

How to exclude resource package from being build into jar Maven?

HI i am building an project into a jar file from maven but i want some resource packages not to be included in that jar. Rather i want to build two seperate jars one that contain project and another with those excluded packages. How to do that?
Assuming you are using dependency:copy-dependencies there are two options that will help you:
excludeClassifiers: Comma Separated list of Classifiers to exclude. Empty String indicates don't exclude anything (default).
excludeScope Scope to exclude. An Empty string indicates no scopes (default).
I use this for example to exclude jsp-api.jar from my Struts2 war project. This jar is needed to run StrutsTestCase tests but if it's included in the war, it conflicts with Tomcat lib directori, wich already contains jsp-api.jar
Setting my dependency as this in my pom.xml:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
And my maven-dependency plugin as this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}/src/main/webapp/WEB-INF/lib</outputDirectory>
<excludeScope>provided</excludeScope>
</configuration>
</plugin>
I've solved my excluding jar problem when deploying on Tomcat

XML maven artifact not on classpath

I have some external configuration (XML files) that are installed in Maven. I need to have them on my test classpath but they aren't appearing.
They must stay as XML, I cannot package them inside a Jar - but I am willing to try anything else for this, custom plugin etc.
(Please don't inform me that Maven is only for Jars - that's simply not true (and if you provide a reference refuting that I can assure you it's out-of-date/misinformation).
The dependencies are specified thus:
<dependency>
<groupId>some.group</groupId>
<artifactId>some.artifact</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>xml</type>
<classifier>some.classifier</classifier>
</dependency>
These XML artifacts have been created by the build-helper plugin (so there's no 1-2-1 with their project's POM).
My only current hacky solution is to, check for the M2_HOME property and load the files from there (as they're defined as dependencies Maven does pull them down) - but I'm not happy with this.
EDIT: The next best hack is probably to use the maven-dependency-plugin to copy these to the output directory (target/classes). If my config is fine for Jars then this smells like a Maven bug.
EDIT 2: #khmarbaise asked for the build-helper-plugin config:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>target/classes/ddl-seed.xml</file>
<type>xml</type>
<classifier>ddl-seed</classifier>
</artifact>
<!-- ... more definitions -->
This generates the correct maven-metadata-local.xml data for all the XML artifacts.
Unfortunately I can find no way of forcing maven to add the test dependency specified to the test classpath, other than this stinky hack of copying it to a directory on the test classpath.
This seems the quickest way (it's for a test dependency), avoiding any JAR creation.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.acme.gid</groupId>
<artifactId>com.acme.aid</artifactId>
<version>${project.version}</version>
<classifier>ddl</classifier>
<type>xml</type>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Apache Commons IO not getting added to jar

I've recently added Apache Commons IO to a small project of mine so that I can tail a log file. Everything works great in my IDE (IntelliJ), but when I create the executable jar, Commons IO isn't in there, so I get :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/io/input/TailerListener.
Commons IO has been added to my POM:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
I've never had issues with added dependencies like this before. What am I missing?
That's not exactly how dependencies work. Dependencies are just there to tell other projects what to pull in. They don't become automatically included in your jar.
The reason for this is because it's wasteful under certain circumstances. Imagine you have two jars: a.jar and b.jar. Both rely on apache commons. It would probably be more efficient (space-wise) to simply have each separated and loaded from a common library directory.
If you want a so-called "fat jar" (or whatever the proper term for having all of your dependencies in one jar is), you need to use a plugin like this one:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.test.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Resources