Maven2: How do I generate a file that contains the names of the project's dependencies? - maven-2

I would like to place the names of the dependencies in a text file that is distributed inside a package that is built with Maven.
I am planning to use the maven assembly plugin to generate the tarball package, and use filtering to put the names in the text file.
The only problem is, I don't know how to reference the dependencies in the first place.

You don't need to use filtering for that, use the Maven Dependency plugin and its a dependency:tree goal to display the dependency tree for this project. Set an output file with the... outputFile optional parameter. So the configuration might look like:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>tree</id>
<phase>prepare-package</phase>
<goals>
<goal>tree</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/dep.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Running the package phase would generate the dependency tree in target/classes/dep.txt and package it in the artifact. Adapt it to suit your needs.

You can use the maven-dependency-plugin dependency:tree to output the tree of dependencies into a file.

mvn dependency:resolve seems to be what you are looking for. Put following plugin configuration to your POM file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>list-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>resolve</goal>
</goals>
<configuration>
<outputFile>dependencies.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
It will produce file dependencies.txt with content similar to:
The following files have been resolved:
am:amagent:jar:1.0:system
am:amclientsdk:jar:1.0:system
aopalliance:aopalliance:jar:1.0:compile
asm:asm:jar:2.2.3:compile
com.sun.jdmk:jmxtools:jar:1.2.1:compile
com.sun.jmx:jmxri:jar:1.2.1:compile
com.sun.xml.bind:jaxb-impl:jar:2.1.12:compile
com.sun.xml.fastinfoset:FastInfoset:jar:1.2.2:compile
com.sun.xml.messaging.saaj:saaj-impl:jar:1.3.2:compile
commons-lang:commons-lang:jar:2.3:compile
commons-logging:commons-logging:jar:1.1:compile
dom4j:dom4j:jar:1.6.1:compile
javax.activation:activation:jar:1.1:provided
javax.jms:jms:jar:1.1:compile
javax.mail:mail:jar:1.4:compile
javax.xml.bind:jaxb-api:jar:2.1:compile
javax.xml.soap:saaj-api:jar:1.3:compile
junit:junit:jar:4.4:test
log4j:log4j:jar:1.2.15:compile

Related

Maven PMD plugin reporting not going to correct directory

I have the following configuration settings under my pom.xml to generate reports for PMD code analysis:
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
<executions>
<execution>
<goals>
<goal>pmd</goal>
</goals>
<configuration>
<outputDirectory>code-analysis/reports/html</outputDirectory>
<targetDirectory>code-analysis/reports/</targetDirectory>
<aggregate>true</aggregate>
<rulesets>
<ruleset>code-analysis/rulesets/pmd-custom-rules.xml</ruleset>
</rulesets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
But after I run mvn pmd:pmd I didn't get the report in the outputDirectory I want. Does setting the aggregate=true only generates 1 report for all the sub-projects under maven? How do I configure it so that it will only generate 1 report to the output directory that I want?
With Maven it is not possible to run a goal by execution(Id), that's only possible by calling it by phase. So if you want to run pmd:pmd from commandline AND have specific configuration, move the configuration outside the executions-block. Now it becomes general configuration. It'll be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
<configuration>
...
</configuration>
</plugin>

Is there any maven goal that is similar to 'dist'?

I'm working on a project that used ant. I had a target dist that would basically do jar first, and then install the application into a directory.
This means, it would create directories like bin/, lib/ and config/ in the installation directory, and then copy the relevant files into each of these directories.
My question is two-fold:
Is there any maven goal that does this kind of thing?
If not, I want to do maven dist and make this happen. How would you suggest I accomplish this using Maven?
If I can't have my own "target" (like dist), then what would be the best way?
Bottom line: I want to do all this, but don't want to alter the behavior of the default "targets" like compile and package etc.
Thanks,
jrh
PS: I'm using maven version 2.2.21
I don't know what would go in config, but lib and bin is easy.
To copy all dependencies to a folder just do this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dist/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
To output your jar to a bin folder do this (reference page):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<outputDirectory>${project.build.directory}/dist/bin</outputDirectory>
</configuration>
</plugin>
Ah, there are additional requirements:
Bottom line: I want to do all this, but don't want to alter the behavior of the default "targets" like compile and package etc.
In this case I'd use a profile to turn this on:
<profile>
<id>dist</profile>
<build>
<plugins>
<!-- insert stuff from above here -->
</plugins>
</build>
</profile>
Now you would do mvn clean package -Pdist to get your dist directory and if you don't add the profile, you get default behaviour.
Basically, things work differently in maven from the way they do in ant. There are no targets, there are only lifecycle phases and plugin goals.
You can either execute a lifecycle phase, which will call all maven plugin goals that are bound to all phases up to this one (e.g. if you do mvn compile, the following phases will be executed: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile). But there is no (easy) way to define a lifecycle phase named dist.
Or you can execute a specific plugin goal (you can actually execute multiple phases and / or plugin goals). E.g. you could write your own dist plugin and call it using mvn dist:dist, but I wouldn't recommend that because you are using existing functionality and the profile solution should be a pretty good fit.
You could try writing an assembly descriptor for the assembly plugin (or search google for a suitable one).
Something like this
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>dist</id>
<formats><format>zip</format></formats>
<fileSets>
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
<files>
<file>
<source>${project.build.directory}/${project.artifactId}.jar</source>
</file>
</files>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
will create a ${project.artifactId}-dist.zip inside your target directory. The zip file will be laid out like
yourProjectName/
yourProjectName/config/...
yourProjectName/lib/...
yourProjectName/${project.artifactId}.jar
It looks like the assembly plugin will only create compressed files, it won't just copy them to a dist folder.
Seans answer is good and I almost went for it until I found out about the appassembler plugin http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/.
See here for an example https://github.com/khmarbaise/maven-training/tree/master/502-assembly-plugin
It is called as part of the package lifecycle.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<binPrefix>utility</binPrefix>
<assembleDirectory>${project.build.directory}/appassembler</assembleDirectory>
<extraJvmArguments>-Xms512m -Xmx1024m</extraJvmArguments>
<generateRepository>false</generateRepository>
<repositoryName>lib</repositoryName>
<repositoryLayout>flat</repositoryLayout>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<programs>
<program>
<mainClass>com.soebes.tools.cli.UtilityCLI</mainClass>
<name>utility</name>
</program>
</programs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/bin.xml</descriptor>
<descriptor>src/main/assembly/bin-unix.xml</descriptor>
<descriptor>src/main/assembly/src.xml</descriptor>
</descriptors>
<tarLongFileMode>gnu</tarLongFileMode>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
The descriptors it references are fairly straigtforward and it also creates a batch and shell script for you to run your application!
You cannot have maven dist, with NetBeans (and other IDEs I believe) you can create a custom action dist which executes as mvn install -Pdist (or mvn clean package -Pdist, as suggested by Sean).

Disable the default-jar execution

I am using Maven Assembly plugin to pack a jar file.
But when I run mvn package, maven always trigger the [jar:jar {execution: default-jar}] to create a default jar file.
So I will have 2 jar files (one created by Assembly plugin and one created by Maven jar which i don't want to be created).
How can I turn off the default-jar execution?
In my pom.xml, I am using: <packaging>jar</packaging>.
I don't want to change it to <packaging>pom</packaging>.
(...) So i will have 2 jar files (one created by assembly plugin and one created by maven jar which i dont want to be created).
Looks like you're doing pretty complicated things. Maybe Maven is not the right tool in your case.
How can I turn off the execution: default-jar.
You can set the <phase> of the corresponding execution to something unknown, like none:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
This seems to work as long as you're providing something else to be installed, like an assembly (I only tested install). But of course, this is a hack.
While not a direct answer to the question, you could exclude the jar created by maven jar using <useProjectArtifact>false</useProjectArtifact>

Can I set the project version with a buildnumber-maven-plugin?

I'm trying to add the svn.revision to project version as a build number and can't seem to do so. My jar has the correct name durin packaging, but its installed in the my local repository it is as if ${buildNumber} is/was undefined when the version was set.
I get foo-1.0.0-SNAPSHOT-${buildNumber} instead of foo-1.0.0-SNAPSHOT-304
Any idea what I'm doing wrong or is adding a revision to the project version a bad idea? Thanks for the help.
<project>
...
<version>1.0.0-${release.identifier}-${buildNumber}</version>
<properties>
<release.identifier>SNAPSHOT</release.identifier>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<id>useLastCommittedRevision</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<useLastCommittedRevision>true</useLastCommittedRevision>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</project>
The problem has two parts:
You're trying to set the buildNumber into the version before it is resolved so it will always be ${buildNumber} rather than the resolved value.
Instead of trying to dynamically change the version, you should set the buildNumber into the finalName element in the build. This will create the artifacts with the intended name in the local repository.
The install plugin will ignore the finalName and deploy it as 1.0.0-SNAPSHOT regardless, I don't know of a way to address that. The buildNumber is added to the Manifest if you configure the plugin as below.
So your configuration would be something like:
<version>1.0.0-${release.identifier}</version>
...
<build>
<finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
...
</build>
I would avoid using build numbers on SNAPSHOT projects.
Maven provides the SNAPSHOT keyword to signify a volatile project in active development. So if you reference a project with a SNAPSHOT dependency version, Maven will automatically check for updates and keep your dependencies in sync.
If you then add a build number to the end of that version, you will have to manually update the dependencies, so you lose any benefit of having the SNAPSHOT suffix.
I personally avoid using build numbers where possible anyway. If I have to update a project, I just bump the version number, or use a suffix like beta-2 or RC2. If you need to track the revision in the SNAPSHOT, I'd recommend adding it to the Manifest so you can check where the build originated, but use the standard SNAPSHOT suffix to allow Maven to resolve the versions normally. The configuration below shows how to add the revision to the Manifest.
As far as your configuration is concerned, it looks OK to me assuming your SCM url is set up correctly. If you have no SCM configuration in your POM that may be the problem.
Can you run with -X and check for any output from the plugin indicating why it isn't setting the property?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>0.9.4</version>
<executions>
<execution>
<id>useLastCommittedRevision</id>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
Add this after the buildnumber-maven-plugin:
<plugin>
<groupId>io.github.michaldo</groupId>
<artifactId>nashorn-maven-plugin</artifactId>
<version>0.0.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>eval</goal>
</goals>
<configuration>
<script>
$project.artifact.version = "${buildNumber}";
</script>
</configuration>
</execution>
</executions>
</plugin>
And the buildNumber will be recognized by packaging and deploy.

Maven in multimodule web project: how to put sibling modules output to WEB-INF/classes and not into WEB-INF/lib as JARs?

I have a multi-module Maven project.
By default when I build a web module, all sibling modules of type JAR it depends on are copied to WEB-INF/lib folder. I want output of sibling modules to be placed in WEB-INF/classes folder without packaging to JAR.
More general question may be: how to keep sibling modules' configuration files out of JARs so that they can be edited after deployment easily?
You could use an overlay, although that requires that the sibling be of type war rather than jar. There's also using the dependency plugin to unpack the jar, but it will only unpack the version in your local repository, not the one you just packaged.
As for your 'more general' question, there's the excludes tag for the jar plugin.
In case if somebody is interested, I found this solution. I had exactly the same issue.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/MYPACKAGE_TO_EXCLUDE.jar</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-MYPACKAGE_TO_EXCLUDE</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>MYPACKAGE_TO_EXCLUDE</artifactId>
<version>${project.version}</version>
<type>jar</type>
<outputDirectory>${project.build.directory}/classes
</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>