How to download the souce of jars to custom location in maven? - 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.

Related

Install über jar with maven-shaded-plugin

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).

Using profiles to exclude WAR modules from 'mvn install'

My parent pom defines 7 modules, 5 of which are dependency jars, and two are wars that depend on those jars.
Question: Is it possible to use maven profiles (or another solution) to define which modules are included when mvn install is run against the parent pom to exclude the two war packages?
I would then like to have a different profile (or another solution) to package the two wars. If that profile is run, the dependency jar modules should be rebuilt and installed only if they are missing from the repository.
You could use the build-helper-maven-plugin in your parent pom.xml file to create a new property based on the packaging (at runtime it would change from pom for the parent, to jar and war for the modules). This new propery could then be used to skip the maven-install-plugin dynamically.
A simple example:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>build-helper-regex-is-packaging-war</id>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>only.when.war.is.used</name>
<value>${project.packaging}</value>
<regex>war</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<skip>${only.when.war.is.used}</skip>
</configuration>
</plugin>
Doing so, the dynamic ${only.when.war.is.used} property would be set to true only when project.packaging would have value war and as such effectively skip the maven-install-plugin executions via its skip option.
You could then move this behavior to a profile and have different settings for jar and war, keeping them in a common place: the root pom.xml, thanks to their dynamic behavior.
Concerning the ability to detect whether an artifact has already been installed or not, there is no such an option on the official plugin documentation and I don't think you could have such a behavior by simply using the plugin.
You could however use the maven profile activation mechanism in case a file is missing (the installed file) and activate the profile accordingly.
You could have in a dynamic way (based only on standard properties) the following approach:
<profiles>
<profile>
<activation>
<file>
<missing>${settings.localRepository}/${project.groupId}/${project.artifactId}/${project.build.fileName}.${project.packaging}</missing>
</file>
</activation>
...
</profile>
</profiles>

Setup Intellij to hot update jars and run Maven Appassembler

I want to setup Intellij to automatically do what I am doing from the command-line with maven repeatedly, which is to run mvn package -DskipTests to rebuild my jar and run the Appassembler Maven plugin to produce my runnable scripts. Ideally, all I want it to do is hot update the classes within the jar which I have changed.
I have figured out how to tell Intellij to create jars with the Artifact tab in Project Structure, but can I get Intellij to import this artifact information from the pom instead of me setting it up manually?
It does auto-import pom changes, but never imported this artifact info.
This would enable it to use the exact output name of what maven produces, so that whether I'm working from the command-line or IDE I can work with one set of outputs. (reason below)
Appassembler adds an additional step, which includes it copying all the dependencies into its target folder and producing the scripts. If Intellij can't trigger Appassembler, I was thinking maybe Appassembler could use symlinks instead and the when the jar as updated, my runnable app scripts would immediately be using that version. Or in the worse case, I only need to run this particular step from the command-line, the jar having already been built.
Update
In case it helps, here's how I use Appassembler in my pom.xml:
<build>
<plugins>
<plugin>
<executions>
<execution>
<id>package</id>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<programs>
<program>
<mainClass>com.foo.bar.Foobnobicator</mainClass>
<name>gofoo</name>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
Thanks for the advice on the best way to achieve this.

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 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