maven attach dependencies - maven

I create a simple maven 109 project - maven-archetype-quickstart. Then, add to pom.xml a dependency to derby.
When I run mvn dependency:tree, I see, that the dependency parsed correctly: [INFO] \- org.apache.derby:derby:jar:10.8.1.2:compile. But when I see the package generated by mvn package, it has only 3.2kB and the dependency is not there. Why? How it works?

Because most people don't want all dependencies in their JAR by default. Imagine the people who want to use your artifact but a different version of derby.
To create an "ueber JAR" (i.e. a JAR which contains everything necessary to run it), use the Maven assembly plugin (search for "Creating an Executable JAR").

Related

How to run Maven Javadoc in a project with pom packaging?

I have a project that packages the delivery of a software using the assembly plugin. The packaging of the project is pom.
To make a nicer documentation i am using the dependency plugin to download the sources of the different projects and then using the javadoc plugin to generate a new documentation that merges the javadoc for the different projects into one.
The issue I am having is that maven javadoc will not run if the packaging is pom.
It complains with the message: Not executing Javadoc as the project is not a Java classpath-capable package
However, if I put packaging jar it works. Unfortunately then an empty unwanted jar file is generated.
Is there a way to get the maven javadoc to run with packaging pom?
Cheers,
Javi
The workaround I have found was to set the packaging in the pom to jar and prevent maven jar plugin to generate the jar.

Force Maven Shade Plugin to apply latest SNAPSHOT dependency in a multi-module build

I am using the maven-shade-plugin in a multi-module build to generate a shaded JAR.
Everything works as expected, except that SNAPSHOT dependencies from my modules do not get updated unless I clean the aggregate project.
pom.xml
|
+-a/pom.xml (a-1.0.0-SNAPSHOT.jar)
|
+-b/pom.xml (b-1.0.0-SNAPSHOT.jar)
|
+-aggregate/pom.xml (a_and_b-1.0.0-SNAPSHOT-jar)
In the example, the aggregate project builds the shaded jar. The aggregate project lists "b" as a dependency, and "b" lists "a" as a dependency. If I make a change to code in "a" and run "mvn install" from the parent directory, "a" is rebuilt but the shaded JAR does not contain the updated "a".
Previously, I used the dependency and assembly plugins to create the fat jar file. The dependency plugin provides this behaviour with the overWriteIfNewer and overwriteSnapshots options. I can do a "mvn clean -pl aggregate; mvn install", but want to know if there is a similar shade plugin option for me to force the shade plugin to pull in newer dependencies?
I am using apache-maven-3.3.9 and version 2.4.3 of the maven-shade-plugin.

What is wrong with my neo4j test setup? EmbeddedNeo4j.java, neo4j, maven

I started a project with maven using the "quickstart" archetype. I then changed my POM to include neo4j:
https://github.com/ENCE688R/msrcs/blob/master/pom.xml
I added:
https://github.com/neo4j/neo4j/blob/master/community/embedded-examples/src/main/java/org/neo4j/examples/EmbeddedNeo4j.java
and ran
mvn package
This works with no errors, but
java -cp target/msrcs-1.0-SNAPSHOT.jar org.neo4j.examples.EmbeddedNeo4j
Returns the Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/neo4j/graphdb/RelationshipType
What am I missing? At this point I simply need to test that I can include and use neo4j.
use
mvn exec:java -Dexec.mainClass=org.neo4j.examples.EmbeddedNeo4j
there is also mvn dependency:copy that copies all dependencies to target/dependencies
and there is the mvn appassembler plugin that allows you to generate startup shell scripts that include all your dependencies as a classpath.
And last but not least there is the maven assembly plugin mvn assembly:single which generates a single jar file that you can run java -jar my-jar-file.jar
You need to add the Neo4j dependencies to your classpath as well. At the moment you're only adding the source jar you created. If you look at this POM you'll see that Neo4J examples require many other dependencies.
Find the libs directory where the dependencies have been downloaded (this may be in your local .m2 maven repo) and add these jars to your classpath. You do not need to add each jar one-by-one as you can simply add a directory with wildcards - ex:
Windows:
java -cp "target/msrcs-1.0-SNAPSHOT.jar;lib/*" org.neo4j.examples.EmbeddedNeo4j
Mac/Unix:
java -cp "target/msrcs-1.0-SNAPSHOT.jar:lib/*" org.neo4j.examples.EmbeddedNeo4j
I've started to work on some maven archetypes which could be a good starting point as well.
For java Neo4j projects, use neo4j-archetype-quickstart.
For Spring Data Neo4j projects, use sdn-archetype-quickstart.

Maven: Deploying a jar/war with built in pom.xml file

I don't have a Maven project. What I have is a jar with the pom.xml embedded in the file. When I deploy this via the Artifactory GUI, Artifactory finds the pom and deploys it with the jar. It even recognizes the groupId, artifactId, and version.
When I try using mvn deploy:deploy-file it fails. If I extract the pom, and run mvn deploy:deploy-file with the -DpomFile=pom.xml, it works. Is there a way of deploying this jar with the embedded pom via the Maven command line? Or, do I need to extract the pom.xml first?
I have not heard of the possibility to specify the pom file from archive. I think it is unlikely to be an option, because Maven itself is just a light-weight program, which runs with plugins; and it needs some configurations to run with; and all references to plugins to be used are in those files.
Consider writing an Ant script that will extract the file, run mvn deploy:deploy-file -DpomFile=pom.xml and then delete the file.
The solution looks not very nice, I know, but it should help.
This is an Artifactory feature and not standard Maven behaviour.
Keep in mind that, for example, if you use dependency:unpack-dependencies or the assembly plugin to create some sort of über jar there would be multiple pom.xml files within the jar under the /META-INF/ path so it would be very difficult to select which pom was the true pom.

Maven-ear plugin: could not resolve dependencies to ejbModule

I am probably missing something but how is the maven ear plugin (I'm using version 2.5) resolving ejbModule dependencies? I have the following structure:
my-parent (multimodule)
-- pom.xml
-- myEar
-----pom.xml
---myEjb
-----pom.xml
In the ear I have the dependency to myEjb.
2 things that are unexpected when i execute mvn package under myEar
It does not build (package) myEjb.
It does try to resolve the depedency to myEjb via repository, which is in our case our custom sonar repository that we configured in settings.xml. I would like not to have to install myEjb to our custom sonar respository in a separate step.
What I expected from the plugin would be: check the ejbModules dependencies, build and package them if not done or something changed, copy the jar to the target directory of the ear project and package the whole thing correctly.
Obviously I am missing something though, anybody can enlighten me?
Based on your structure you have to build from the root of your project which means you have to change to my-parent directory
my-parent (multimodule)
-- pom.xml
-- myEar
-----pom.xml
---myEjb
-----pom.xml
and do
mvn clean package
The behaviour you described that maven tries to solve the dependencies against your local repository is the usual process. You can do partialy building a single module within a multi-module build but in your case this won't work cause you depend on your myEjb which neeeds to be rebuilded. Otherwise you can do a
mvn install
first and after that you can do from the root:
mvn -pl myEjb clean test
which will only build the myEjb module and uses the dependencies from the repository (localy in this case).

Resources