Switching "maven-assembly-plugin" on/off - java

Our current DevOps environment runs mvn package and auto-deploys the jar artifact in a private Maven repository and all target folder is cached for later use. But the project has also a maven-assembly-plugin set up what packages a second jar file (a fat jar suffixed as -jar-with-dependencies).
We don't want the "fat jar" to be generated by maven-assembly-plugin and so stored in that cache along with other files in that case.
Is there a way to switch maven-assembly-plugin on and off by command line (or any other property or environment variable) to run it only when explicitly required?

You can set the property assembly.skipAssembly to true.
Either on command line ( with -Dassembly.skipAssembly=true) or in the POM itself.

The easiest approach (IMHO), would be to define the plugin in its own profile. Inside your pom.xml, you add a profiles section, and in that a profile that would include the plugin:
<profiles>
<profile>
<id>assemble</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- All the configuration you have for the plugin -->
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then, by default, this plugin will not be called. When you want to call it, you can explicitly enable this profile with the -P flag:
mvn package -Passemble

Related

maven: execute without actually building anything

I have a project with finalised version in pom files , lets say 12.3.45 .
I have built the code for this version some time ago already, all the built jars are in the local maven repo.
Then at some point I have run mvn clean, so all the target folders are being removed.
And now I want to execute some code, as quickly as possible, using mvn exec:java. Preferably without building anything, because why not? all the jars at some point were already built, and I know there were no code changes after that. How can I force maven to execute the code as fast as possible , not recompile anything, and just reuse the jars from the local repo?
Thanks.
If your artifacts are in a local or remote repository you can use them as dependencies.
You can use exec-maven-plugin's options includeProjectDependencies or includePluginDependencies to use them in java execution
https://www.mojohaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies. includeProjectDependencies option is enabled (true) by default.
You can execute exec-maven-plugin without building anything with mvn exec:java command
Instructions:
To run exec-maven-plugin you would need a main class to run. I assume you have one in your project. If you don't - you need to make a separate project with a main class.
Create a blank maven project.
In the project add exec-maven-plugin configuration. Set the mainClass
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pack.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Include you artifacts as dependencies to the project
<dependencies>
<dependency>
<groupId>my.group</groupId>
<artifactId>myartifact</artifactId>
<version>12.3.45</version>
</dependency>
</dependencies>
Run mvn exec:java to execute com.my.package.MyMainClass main class from my.group.myartifact artifact
Edits:
includeProjectDependencies option is enabled (true) by default

Is there a Maven plugin that runs a non-fat jar?

Imagine a normal java maven project with a Main class that produces the artifact project-a.jar. This project has a dependency on project-b.jar.
Is there a Maven plugin that allows to run that jar by a command like that?
mvn run-plugin:run org.mygroup:project-a:3.1 <args>
The plugin would resolve the runtime dependencies (using META-INF/maven/(...)/pom.xml), install the project and its dependencies to the local maven repository (if not already there), construct the classpath and invoke
java -cp (...)/project-a-3.1.jar;(...)/project-b-2.1.jar org.mygroup.Main <args>
I know that the usual way is to build an executable (fat) jar that contains the dependencies, but that's not what I am asking for.
Actually, it is not even necesary to read the pom from the jar, because maven can download it from the repositories given the coordinates.
Why this question is different to the Maven Run Project question:
I do not want to start from having the project's source already checked out. So the usual use of the exec plugin is not applicable. The OP of the Maven Run Project question obviously assumed the presence of a source code project folder. Her purpose was testing and she accepted an answer that clearly needs a project. The wording of both questions is correct, too. There is a difference between the words "project" and "jar" and their actual meaning in their respective contexts is quite different.
You can use the appassembler-maven-plugin plugin, it creates a shell script that has the dependencies in the classpath for you. Heres an example config
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<extraJvmArguments>-Xms256m -Xmx1536m</extraJvmArguments>
<programs>
<program>
<mainClass>com.package.MyMainClass</mainClass>
<name>TestFormattingUtils</name>
</program>
</programs>
</configuration>
</plugin>
You can find the output script in .../target/appassembler/bin You can manually inspect the script and you'll see that its doing the type of command you wanted where it adds the jars to classpath via the command line. ie java -jar (...)/project-a-3.1.jar -cp (...)/project-b-2.1.jar <args>
I'm not a fan of jars-in-jar either, but I do maintain various tools with lots of dependencies. So, at one point, I decided to write an executable AppBoot jar which puts all the jars from a lib-subdirectory in a class-loader and then calls the main-method of the desired (executable) jar. This question prompted me to investigate if the exec-maven-plugin could do something similar, and it can.
The exec-maven-plugin does not require a "Java project" directory, but a pom.xml in a directory is required. The pom.xml I used is shown below, note that it can be placed in any (empty) directory and the application can be started by opening a shell/prompt in that directory and executing mvn exec:exec. Use mvn -X exec:exec to review the classpath used by the exec-maven-plugin.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.descartes</groupId>
<artifactId>exec-embed-demo</artifactId>
<version>1.2.2-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- Start the demo using Maven repository artifacts, execute with "mvn exec:exec" -->
<properties>
<demo.version>1.2.1.GH</demo.version>
<mainclass>com.descartes.basicjsp.embed.demo.Launch</mainclass>
<appname>${project.artifactId}</appname>
<homedir>${project.basedir}/</homedir>
</properties>
<dependencies>
<dependency>
<!-- exec-maven-plugin will get all required (runtime) jar-files from this dependency. -->
<groupId>com.descartes</groupId>
<artifactId>basic-jsp-embed-demo</artifactId>
<version>${demo.version}</version>
</dependency>
</dependencies>
<build>
<!-- The "outputDirectory" is added to the classpath by the exec-maven-plugin. -->
<!-- Add this pom's directory to the classpath instead of "./target/classes". -->
<!-- The directory should contain "logback.xml" to prevent a million lines of debug output from Tomcat. -->
<outputDirectory>${homedir}</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<!-- mvn exec:exec configuration -->
<!-- Embedded Tomcat will not stop with "ctrl-c" -->
<!-- Use http://localhost:8080/shutdown instead -->
<configuration>
<executable>java</executable>
<arguments>
<argument>-Dapp.name=${appname}</argument>
<argument>-Dapp.home.dir=${homedir}</argument>
<argument>-Dapp.conf.dir=${homedir}</argument>
<argument>-cp</argument>
<classpath/>
<argument>${mainclass}</argument>
</arguments>
</configuration>
<!-- mvn exec:java configuration -->
<!-- "ctrl-c" stops Tomcat but embedded Tomcat fails to start properly, probably a classloader issue. -->
<!--
<configuration>
<mainClass>${mainclass}</mainClass>
<systemProperties>
<systemProperty>
<key>app.name</key>
<value>${appname}</value>
</systemProperty>
<systemProperty>
<key>app.home.dir</key>
<value>${homedir}/</value>
</systemProperty>
<systemProperty>
<key>app.conf.dir</key>
<value>${homedir}/</value>
</systemProperty>
</systemProperties>
</configuration>
-->
</plugin>
</plugins>
</build>
</project>
AppBoot is part of the basic-jsp-embed project that uses embedded Tomcat and that project can be found here (to install, download the latest release, unpack the zip-file and run "mvn install" in the root directory of the multi-module project).
On a side-note: managing a jar-set is tricky, use tools like jHades to verify you will not run into trouble with multiple versions of the same class in different jar-files.
You are looking for the maven exec plugin.
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"]
would run your program
Maven can not do what you want, simply because it has no way to resolve the dependencies of project A once it has been built into a final jar.
Maven does not magically download libraries from the Internet: what makes it work are the definition of repositories inside the pom.xml. Without pom.xml, like you seem to suggest, how would it know where to download libraries from? Maven is not a downloading tool, it is a project management tool and what you have is no longer a project but a final library.
Since you have control over project A, you should really rely on Maven conventions and either build a fat jar or an assembly (with maven-assembly-plugin).
By the way, the pom.xml file located under META-INF is not guaranteed to exist, and, in fact, it is not there if you look at Spring artifacts. Take a look at Maven Archiver documentation: the presence of this pom file is controlled by the addMavenDescriptor boolean attribute. Set this attribute to false and your main artifact will not have this pom file.

POM for Maven Compiler Plugin Jar is missing

EDIT: I've just updated my Eclipse installation from Kepler to Luna - all of my Maven projects were running fine before the update
I am receiving the error which is preventing my Maven project from installing:
The POM for org.apache.maven.plugins:maven-compiler-plugin:jar:2.5.1 is missing, no dependency information available
And when I navigate to Maven's Lifecycle Mapping in Eclipse I see
compiler:compile | error
compiler:text Compile | error
I have maven-compiler-plugin:jar:3.1 (including the POM file) in my .m2 repository and would like to use that instead.
How could this be configured in Eclipse? Alternatively, if this is not the solution to the problem, how could this be resolved?
You need to edit your pom.xml to set the version of the compiler plugin to be used. As described on the plugin's homepage, you configure the maven-compiler-plugin in the build-section of your project's pom.xml like so:
<project>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
...
</project>
If you want to use the local repository of your Maven installation (instead of Eclipse's embedded version), go to Preferences -> Maven -> Installations and add your Maven installation there.

MAVEN , How can remove JSP page based on environment

I am building one war file using mvn clean install -Dlifecycle=dev . so i have variable lifecycle.
Now my requirement is , when i create build file for UAT/PROD it must exclude one jsp(index.jsp) from package .My jsp is in webApps directory parallel to resources.
Using profile only for one page filtering is not good idea i think.
Appreciate any help .
It starts with one JSP. Next is a customized CSS. Then different DB properties...
A profile is the way to go. Just create one, set its activation to the value of the variable, create another source folder with the JSP and add it to the resources in the profile.
So:
Create a folder src/dev/webapp in your project folder (so it is parallel to src/main/webapp)
Add a profile to your pom.xml that configures the war plugin
<profiles>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResources>
<resource>
<directory>src/dev/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This copies the resources from src/dev/webapps into the merged target folder when the variable lifecycle is set to dev.
Even if those are quite some lines of XML for copying a single file I think it is not a good idea to do it different (e.g. with a plugin that deletes files) when using Maven. While you can customize Maven builds so they aren't recognizable any more, the whole idea is to use the conventions so others can easily read the process.

Maven – Always download sources and javadocs

Is there a way I can configure maven to always download sources and javadocs? Specifying -DdownloadSources=true -DdownloadJavadocs=true everytime (which usually goes along with running mvn compile twice because I forgot the first time) becomes rather tedious.
Open your settings.xml file ~/.m2/settings.xml (create it if it doesn't exist). Add a section with the properties added. Then make sure the activeProfiles includes the new profile.
<settings>
<!-- ... other settings here ... -->
<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
</settings>
Edit: As mentioned by Jingguo Yao, this works with Eclipse IDE only - the same can also be configured in your IDE of choice. In Elcipse via Window -> Preferences -> Maven menu, though this probably has to done at every workspace level and for fresh Eclipse installations.
Alternatively configure the maven-dependency-plugin in your pom.xml in a separate profile and run it as required - keeping it in the main build will lead to build times (needlessly elongating (not to mention space) at places like your build nodes that don't need either sources or java docs. Preferable this should configured in some org or division parent pom.xml, otherwise it has be repeated everywhere in different places
In my case the "settings.xml" solution didn't work so I use this command in order to download all the sources:
mvn dependency:sources
You also can use it with other maven commands, for example:
mvn clean install dependency:sources -Dmaven.test.skip=true
To download all documentation, use the following command:
mvn dependency:resolve -Dclassifier=javadoc
Just consolidating and prepared the single command to address source and docs download...
mvn dependency:sources dependency:resolve -Dclassifier=javadoc
Answer for people from Google
In Eclipse you can manually download javadoc and sources.
To do that, right click on the project and use
Maven -> Download JavaDoc
Maven -> Download Sources
I am using Maven 3.3.3 and cannot get the default profile to work in a user or global settings.xml file.
As a workaround, you may also add an additional build plugin to your pom.xml file.
<properties>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
</properties>
<build>
<plugins>
<!-- Download Java source JARs. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
On NetBeans :
open your project explorer->Dependencies->[file.jar] rightclick->Download Javadoc
As #xecaps12 said, the simplest/efficient approach is to change your Maven settings file (~/.m2/settings.xml) but if it is a default settings for you, you can also set it like that
<profile>
<id>downloadSources</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
In Netbeans, you can instruct Maven to check javadoc on every project open :
Tools | Options | Java icon | Maven tab | Dependencies category | Check Javadoc drop down set to Every Project Open.
Close and reopen Netbeans and you will see Maven download javadocs in the status bar.
To follow up on the answer from kevinarpe this does both sources and Javadocs:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>sources</goal>
<goal>resolve</goal>
</goals>
</execution>
</executions>
<configuration>
<classifier>javadoc</classifier>
</configuration>
</plugin>
</plugins>
</build>
I think it can be done per plugin. See this chapter from the Maven book.
You might be able to configure the dependency plugin to download sources (even though I haven't tried it myself :-).
Simply modify file mvn (or mvn.cmd if in windows) and add whatever command line switches you need (as mentioned by other answers). If you don't want to modify the install files (which I'd recommend), create a mymvn (or mymvn.cmd) wrapper that invokes the regular mvn with the parameters.
Not sure, but you should be able to do something by setting a default active profile in your settings.xml
See
See http://maven.apache.org/guides/introduction/introduction-to-profiles.html
I had to use KeyStore to Download the Jars. If you have any Certificate related issues you can use this approach:
mvn clean install dependency:sources -Dmaven.test.skip=true -Djavax.net.ssl.trustStore="Path_To_Your_KeyStore"
If you want to know how to create KeyStores, this is a very good link:
Problems using Maven and SSL behind proxy
For the sources on dependency level ( pom.xml) you can add :
<classifier>sources</classifier>
For intellij users, inside the pom.xml file, right click anywhere and select Maven -> Download sources and Documentation.

Categories

Resources