Install über jar with maven-shaded-plugin - maven

I have been using maven assembly plugin to create uber jar and deploy to Artifactory.
I switched to maven shade plugin to shade some dependencies.
Now my jar is not deployed during install phase.
In the maven assembly plugin documentation:
When the assembly is created it will use the assemblyId as the
artifact's classifier and will attach the created assembly to the
project so that it will be uploaded into the repository in the install
and deploy phase.
This is not a case for shaded plugin.
How to configure maven pom to deploy uber jar created with shaded plugin?

You have to tell maven-shade-plugin to attach the shaded artifact which can be done via:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jackofall</shadedClassifierName> <!-- Any name that makes sense -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

I also had this problem, where mvn install would build a shaded jar in my target directory but install the non-shaded one to my local Maven repository.
What ultimately proved to be the cause was that I had <finalName> defined in my maven-shade-plugin configuration. That ended up saving the shaded jar under that specific name, while the non-shaded jar took the default jar name which mvn install must look for when it comes time to install. Without <finalName> present, it copied the shaded jar appropriately into the local Maven repository.
With <shadedArtifactAttached>, I could get the shaded jar installed, but only suffixed with shadedClassifierName while the non-shaded jar was present under the normal artifact name, causing libraries dependent on it to pick up the non-shaded jar over the shaded one (which was not what I wanted in this case as I'm building a library with some dependencies shaded).

Related

how to Include source code inside the mvn built jar

We are changing our projects from ant to mvn build.
In the ant build jar - xyz.jar [we used to have the source files inside]
xyz.sources.jar inside xyz.jar
How can I do the same through pom.xml. I tried maven-source-plugin, but this creates the sources jar inside target folder. I want this sources jar inside output jar.
Thanks.
The convention is to ship these artifacts separately. Offering them separately in a Maven repository allows tools like Eclipse and IntelliJ to match the sources to the binaries automatically, and life is good.
To do what you want to do, you could run the Maven Source Plugin before the main JAR file is packaged (e.g. in the prepare-package phase), and have it write the sources JAR to the target/classes/ folder, and not attach. Like so:
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>source-jar</id>
<goals>
<goal>jar</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<finalName>filename-of-generated-jar-file</finalName>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>

How to download the souce of jars to custom location in maven?

This is follow up post of How to get all the specified jars mentioned in the pom.xml and transitively dependent jars?
Except that I am looking to download the source of the both dependent and transitively dependent jars to custom mentioned location.
I have tried following command but it didn't works.
mvn dependency:sources -DoutputDirectory=.../
It didn't worked.
mvn dependency:sources dependency:copy-dependencies -DoutputDirectory=.../
It didn't worked.
The source jar is normally available via Maven using a classifier, so that for the same Maven coordinates (GAV, groupId, artifactId, version) you can have more than one artefact related to the same build (i.e. default application/library jar, sources jar, test sources jar, javadoc jar, etc.), as also explained in another SO answer. The standard classifier for sources is sources, created by the Maven Source Plugin.
The copy-dependencies can be configured to fetch a certain classifier via the classifier option.
So in your case, to get the sources of your dependencies to an external folder, you can invoke the command as following:
mvn dependency:copy-dependencies -DoutputDirectory=somewhere -Dclassifier=sources
Note the additional -Dclassifier=sources option.
An example of pom configuration to achieve the same is also explained in the official documentation of the Dependency Plugin, using the following snippet:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>src-dependencies</id>
<phase>package</phase>
<goals>
<!-- use copy-dependencies instead if you don't want to explode the sources -->
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Beware though that Maven doesn't know about sources, it only knows about artefacts. So if the sources (classified) artefact is not available via its GAV+C, Maven will find it and as such will not download any source.

Maven dependencies from parallel projects, not modules.

I have following maven projects (note : maven project, not maven modules). I know i will get questions, why not modules, but it has its own story, anyway.
myproj-common (JAR),
myproj-core (JAR),
myproj-product1-batch (EAR),
myproj-product2-batch (EAR)
myproj-core depends on myproj-common, and myproj-product1-batch depends on myproj-core.
If it is modules, i can simply create dependency. It if is standard archive, I can also create dependency and have JAR available in repository, if it is a library JAR, I can ...bla bla bla ...all standard dependency I can fix, I am not sure how to make a jar that is sitting somewhere on the disk, a dependency.
I am not able to get
C:\Documents and Settings\users\myproj-common\target\myproj-common-0.0.1-SNAPSHOT.jar
into following jar, as a dependency
C:\Documents and Settings\users\myproj-core\target\myproj-core-0.0.1-SNAPSHOT.jar
same problem for the JAR into EARs.
Any idea ? How ...hoping a small, quick and surprising fix, just not visible to me.
I don't see any reason why you shouldn't be able to use mvn install to install these jars and ears into your local repository. Then, you can just include them as dependencies anywhere you like.
I doubt that you'll be able to cleanly get maven to pull in a jar sitting anywhere on your filesystem other than your local repository.
To clarify, when you do mvn install it puts your JAR in the target folder, but it also puts it into your local repository (C:\Documents And Settings\.m2 by default in Windows). After that JAR is installed there, you can include that JAR in other projects as a maven dependency using a "dependencies" block in your pom like this:
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
The dependencies mechanism is explained in the Maven Docs.
In addition, you'll need to tell maven to actually package all dependencies in the JAR. You can do that by adding this to your POM. (Also check this question How can I create an executable JAR with dependencies using Maven? for the source of this code block).
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>

Maven Artifact download?

I'm new to maven world. My organization has maven release process all the artifacts are kept in our maven remote repo, so I want to deliver some specific artifact version to some of our customer. How can I download specific module version?
In case you don't have a web interface like Nexus for your internal repo, you can create a standalone pom.xml, enter the desired version into the dependencies and call mvn package.
If you need some sample poms and extra info to get you started, you can look here and here
EDIT : I forgot you might expect the artifact to turn up in the same folder as the pom is in. The default maven setting is to download it to a subfolder of /yourHomeFolder/.m2/repository/The name of the subfolder is the group Id of the artifact.
EDIT2: here is a sample setup for downloading the jars into the folder of your choice. If you delete the <outpuDirectory> setting, the artifact jar will be downloaded into the subfolder /dependendies. The execution is bound to the validate phase, so just call mvn validate
<build>
...
<plugins>
<!-- Dependency plugin to download the configured dependencies (transitive). -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<execution>
<execution>
<id>download-jar</id>
<phase>validate</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>put/your/directory/here</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Maven war plugin

Is it possible to have the maven war plugin output to two different locations? I currently have the following in my pom.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<webappDirectory>${webappDirectory}</webappDirectory>
</configuration>
</plugin>
This was already existing in the POM for the gwt maven archetype, and I'm guessing this explodes everything into the webappDirectory(which the gwt plugin then uses for it development mode).
When I do a
mvn war:war
It generate a war file for me in the target directory. So, I suspect its a different plugin configuration than the one in my POM (default behaviour?). How do I override this?
I basically want to accomplish the following:
I would like to have two different resource folders "src/resources/a" and "src/resources/b" , and have one of the folders used in the exploded version (currently in my pom) and the other version used when I do a "mvn war:war"
Per this question How to execute maven plugin execution directly from command line?, Maven doesn't use pom configuration when you invoke a plugin directly (e.g. mvn war:war). Your POM config is telling Maven to run the exploded goal when the compile phase is invoked (i.e when you run mvn [phase] where phase is compile or later).
I suggest you investigate using a separate profile for exploded deployment (called eg exploded), with a different configuration of the resources plugin to copy a different resources directory. Then use mvn compile -Pexploded for the exploded version.

Resources