how to generate download-site using maven - maven-2

I am new to Maven and trying to accomplish a simple task:
build jar package and web site [DONE]
deploy them to remote server via scp [DONE]
the site should contain download page with links to the deployed jar files [MISSING]
I do not want to use archiva or similar tools. I just want to have a (static, generated) page on the web site with the links to all the built jars (or only to the latest build).
I tried:
put <item name="Downloads" href="download.html"/> into the site.xml
mvn commons:download-page
mvn deploy site:deploy
What I get:
these commands copies the jar file to remote server:
{deploy-dir}/com.acme.etc/ArtifactID/0.0.2-SNAPSHOT/ArtifactID-0.0.2-SNAPSHOT.jar
the generated download page points to
{deploy-dir}/target/site/[preferred]/commons/ArtifactID/binaries/ArtifactID-bin.tar.gz
Also there are some labels in the generated download page, like [if-any logo][end]. I suppose server should execute the script instead of displaying html. However, I can not do this as the server does not belong to me.
I guess there is a simple way (maybe totally different) to accomplish this task, could you please point out how to do it in the most simple, but automated way.
Thank you.

After deployment has started, the project metadata contains the final version. Here is a pom excerpt with a groovy script that builds a link page based upon this metadata and deploys it afterwards using deploy:deploy-file
<properties>
<!-- The base URL of your repository -->
<linkpage.repobaseurl>http://www.my.maven.repo/public</linkpage.repobaseurl>
<!-- The download page file -->
<linkpage.file>${project.build.directory}/downloadpage.html</linkpage.file>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>build-links</id>
<phase>deploy</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<![CDATA[
def uniqueVersion = null;
pom.artifact.metadataList.each{
if(it.class.simpleName=='ProjectArtifactMetadata'){
def afi = it.class.superclass.getDeclaredField('artifact');
afi.accessible = true;
// this is the final version we need for the URLs
uniqueVersion = it.artifact.version;
}
};
def repoBase = pom.properties['linkpage.repobaseurl'];
def downloadPage = new File(pom.properties['linkpage.file']);
// build list of artifacts
def listOfArtifacts = [];
// main artifact
listOfArtifacts.add(pom.artifact);
// attached artifacts like sources, javadocs etc
listOfArtifacts.addAll(pom.attachedArtifacts);
def str = '';
listOfArtifacts.each{
def cls = it.classifier != null ? '-' + it.classifier : '';
def vers = (uniqueVersion != null ? uniqueVersion : it.version);
def parentPath = "${repoBase}/${ pom.groupId.replace( '.' , '/' )}/${pom.artifactId}/${pom.version}/"
def path = "${parentPath}${pom.artifactId}-${vers}${cls}.${it.type}"
// build the link using a here document
str += """
${it}<br />
"""
}
// now build the page using a here document
downloadPage.text="""
<html>
<head>
<title>Download page for ${project.artifact}</title>
</head>
<body>
<h1>Downloads</h1>
${str}
</body>
</html>
""";
]]>
</source>
</configuration>
</execution>
</executions>
</plugin>
<!-- now we need to manually deploy the download page using deploy:deploy-file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>deploy</phase>
<id>deploy-downloadpage</id>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${linkpage.file}</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<classifier>download</classifier>
<packaging>html</packaging>
<uniqueVersion>false</uniqueVersion>
<url>${project.distributionManagement.repository.url}</url>
<repositoryId>${project.distributionManagement.repository.id}</repositoryId>
</configuration>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
</plugins>
</build>

(...) the site should contain download page with links to the deployed jar files [MISSING]
To my knowledge, this is not supported out of the box and I'm not aware of any plugin doing this (the commons-build-plugin is a specific plugin for Apache Commons builds).
You're best best is to add a custom page including Maven expression to the Maven site and to filter it to generate links to the latests version of artifacts. This is actually what the Maven site itself is doing. From the Creating Content documentation:
Filtering
Note: This feature is available in version 2.0-beta-6 or later of the
Site Plugin.
To filter properties into any
supported documentation format, add a
.vm extension to the filename.
For example, the module for the Maven
website contains a
src/site/apt/download.apt.vm
file, which uses the expression
${currentVersion} to filter in a
property set in the POM
Note: Velocity is used to apply the filtering. Because Velocity uses a
dot-notation internally you can not
use dots in your properties.
If you declare these properties in
your POM
<properties>
<!-- This will not work because the name of the property has a dot in it -->
<my.property>My value</my.property>
<!-- This will work because the name of the property has no dot in it -->
<myProperty>My other value</myProperty>
</properties>
and then use the expression
${my.property} in your document, it
will not work. If you instead use the
expression ${myProperty} it will
work just fine.

Related

How to create folder for generated sources in Maven?

I have to generate sources using wsimport and i assume that it should go to /target/generated-sources/wsimport rather than /src/main/java.
The problem is that wsimport needs target folder created before execution and it fails. Can I create that dir first using any maven plugin. I can do it using ant but i prefer to keep it in POM.
Try using the add source goal of the build helper plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated/src/wsimport</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I have to generate sources using wsimport and i assume that it should go to /target/generated-sources/wsimport rather than /src/main/java.
This is a correct assumption.
The problem is that wsimport needs target folder created before execution and it fails. Can I create that dir first using any maven plugin. I can do it using ant but i prefer to keep it in POM.
I never noticed this problem (and would consider it as a bug, a plugin has to take care of such things).
The weird part is that WsImportMojo seems to do what is has to by calling File#mkdirs():
public void execute()
throws MojoExecutionException
{
// Need to build a URLClassloader since Maven removed it form the chain
ClassLoader parent = this.getClass().getClassLoader();
String originalSystemClasspath = this.initClassLoader( parent );
try
{
sourceDestDir.mkdirs();
getDestDir().mkdirs();
File[] wsdls = getWSDLFiles();
if(wsdls.length == 0 && (wsdlUrls == null || wsdlUrls.size() ==0)){
getLog().info( "No WSDLs are found to process, Specify atleast one of the following parameters: wsdlFiles, wsdlDirectory or wsdlUrls.");
return;
}
...
}
...
}
Could you show how you invoke the plugin and its configuration?

How to add a file to a war with Maven

I have developed a maven plugin that downloads the release notes from JIRA.
It's bound by default to the 'generate-sources' phase and creates a 'release.txt' file in the build folder (${project.build.directory}).
My question: how can I add this file in the 'WEB-INF' folder of the war file built by Maven ?
I know I can use the 'maven-war-plugin' to include additional external resources from the 'src' folder, but I don't want my 'release.txt' file generated there (=not commitable to svn).
Thanks for your help. I wish you a nice day!
Maxence
I think this can be done using this feature of that plugin:
Adding and Filtering External Web Resources:
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
Which would allow you to generate your release.txt into a separate folder (not src) and have the plugin treat it as an extra resources folder.
Hope that helps.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}</directory>
<targetPath>WEB-INF</targetPath> <!-- introduced in plugin v 2.1 -->
<includes>
<include>release.txt</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>

How to run jetty:run-war using a war defined by maven coordinates?

Background: I'm setting up a functional tests module in a maven project. We use the maven-jetty-plugin for testing.
I've got the jetty plugin set up as described here (to play nicely with the Failsafe plugin), but what I'd like to do is deploy the war artifact from our main web module using jetty (which has just been installed into the local maven repo by the time the functional test module is running).
The jetty plugin's run-war goal has a <webApp> element which takes a string path to a war to deploy. I'd much rather specify the war to deploy using the maven coordinates defined by our web module. Is there any way to do this?
Possible workarounds:
Section 4.13 of "Better Builds with Maven" describes using cargo to deploy a war specified using maven coordinates, but that's serious overkill given that we're using jetty.
More reasonable IMO is using dependency:copy to copy the just-built-and-installed war artifact to a fixed path in the functional tests module's target directory, which I can then provide in the jetty plugin's <webApp> configuration element.
The jetty plugin's run-war goal has a element which takes a string path to a war to deploy. I'd much rather specify the war to deploy using the maven coordinates defined by our web module. Is there any way to do this?
This is not really the maven jetty plugin is supposed to be used, the plugin deploys the war of the current module, what you want to do is not supported by default.
Section 4.13 of "Better Builds with Maven" describes using cargo to deploy a war specified using maven coordinates,
Yes, Cargo can do this in a clean way.
but that's serious overkill given that we're using jetty.
I don't agree. First, the jetty plugin doesn't support what you want to do out of the box (so it may not be the right tool). Second, serious overkill is highly exaggerated, a misconception actually, especially given that cargo requires very little configuration (zero?) for an embedded Jetty.
More reasonable IMO is using dependency:copy to copy the just-built-and-installed war artifact to a fixed path in the functional tests module's target directory
No offense but your whole question sounds a bit like: I have a hammer, it was fine for a nail, can I use it for a screw given that getting a screw driver seems a serious overkill? To answer this question (which is somehow what you are saying), you can use dependency:copy and get the whole thing working with the maven jetty plugin, but this is a hack (and since you're actually not asking any question, I guess you wanted an opinion on this). Of course the final decision belongs to you :)
Just in case, here is how I would implement this with Cargo:
<dependencies>
<dependency>
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<type>war</type>
<version>war version</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<!-- Container configuration -->
<container>
<containerId>jetty6x</containerId>
<type>embedded</type>
</container>
<!-- Configuration to use with the container or the deployer -->
<configuration>
<deployables>
<deployable>
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<type>war</type>
<properties>
<context>war context</context>
</properties>
</deployable>
</deployables>
</configuration>
<!-- Don't wait, execute the tests after the container is started -->
<wait>false</wait>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
And I don't think that this can be objectively qualified as a "serious overkill".

Maven 2 checkstyle plugin version 2.5 - Problem with configLocation

I am using checkstyle plugin in maven 2. I now want to switch my config file, from the default one to a) an online file, or b) a local file. I tried the following two things, which both didnt work. Any suggestions?
A) Local file, which is directly in my project folder next to the pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>
B) Remote file, that is stored on a server
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation>
</configuration>
</plugin>
Both cases result in an error like this:
[INFO] An error has occurred in
Checkstyle report generation. Embedded
error: Failed during checkstyle
execution Could not find resource
'file:checkstyle.xml'.
Any help would be appreciated!
I've seen several issues related to configLocation in Jira with the version 2.5 of the plugin (like MCHECKSTYLE-129 or MCHECKSTYLE-131), both a) and b) just work fine with the version 2.4.
So, unless you're using Maven 3, I suggest to rollback to 2.4 for now:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.4</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugins>
or
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.4</version>
<configuration>
<configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation>
</configuration>
</plugin>
As a side note, for a multi-modules build, have a look at the Multimodule Configuration.
I've been trying to use version 3.0.1 of the Checkstyle plugin and found configLocation has no effect. Tried the approach above, but still no luck.
To summarise, the answer above probably does work, but you might need to set a property checkstyle.config.location.
Using -X to get debug output, I saw:
[DEBUG] (f) configLocation = config/sun_checks.xml
Scrolling further back in the log, it looks like configLocation isn't being set:
<configLocation default-value="config/sun_checks.xml">${checkstyle.config.location}</configLocation>
Based on that message, I set the property in the global <properties> as follows:
<checkstyle.config.location>${basedir}/config/checkstyle-configuration.xml</checkstyle.config.location>
This worked, but caused the plugin to throw an exception. After some Googling, I added the following to the checkstyle configuration file:
<module name="Checker">
...
<module name="TreeWalker">
...
<property name="cacheFile" value=""/>
For completeness, the last step came from the following Jira, marked as resolved in 2.8. The difference is it seems to work with an empty value, avoiding the need to set up a ${cachefile} property:
http://jira.codehaus.org/browse/MCHECKSTYLE-159
Maybe helpful for someone else who needs to still find a workaround.
By the way i had the same problem and the file is suppose to be searched in /classes/.xml or folders from here. But since it is looking directly after the project folder i included
<configuration>
<configLocation>src\main\resources\checkstyle-checker-dev.xml</configLocation>
</configuration>
Note: configLocation has L caps
Also you can define a global variable in environment and use here
Note: This is only a workaround, it needs to work as stated in the above lines.

Maven2: How to stage JXR plugin result when using mvn site?

I have a multi-module project and I want to deploy on the project's site an HTML version of my source code using the JXR maven plugin.
The problem is that the JXR plugin runs well, the XREF folder is properly generated for each of my module, but when I use the mvn site:stage command in order to retrieve all the project's site content and to have all link properly generated it does not retrieve the XREF folders.
Here is an extract of my POM file where the JXR plugin is configured:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jxr-maven-plugin</artifactId>
<configuration>
<aggregate>true</aggregate>
</configuration>
</plugin>
Here is the command I use to create and stage my site:
mvn site site:stage
Do you guys have any idea?
Thanks in advance.
r.
Not sure this is relevant, but your command is running the site twice, mvn site will generate the site, and site:stage will also run the site, perhaps this is causing problems but I honestly can't see why.
Looking at the JXR documentation, it only mentions the site:site goal, I can't see why it wouldn't be run properly for the site:stage goal as it extends it. If you run the site goal, then copy the output to another directory, run the site:stage goal and compare the output it might give some insight into the problem.
Update: I tried this myself and the xref was included and aggregated nicely in c:\test\stage with the cross references correctly managed. I've included the configuration I used.
In my parent pom I defined the site configuration like this:
<build>
<plugins>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>stage</goal>
</goals>
</execution>
</executions>
<configuration>
<stagingDirectory>c:\test\stage</stagingDirectory>
</configuration>
</plugin>
</plugins>
</build>
The distributionManagement section was configured with the site information (not really needed as I set the stagingDirectory above, but the goal won't run without it).
<distributionManagement>
<site>
<id>mojo.website</id>
<name>Mojo Website</name>
<url>scp://test/</url>
</site>
</distributionManagement>
My JXR configuration in the parent pom was as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<reportSets>
<reportSet>
<id>src-xref</id>
<reports>
<report>jxr</report>
</reports>
</reportSet>
<reportSet>
<id>test-xref</id>
<reports>
<report>test-jxr</report>
</reports>
</reportSet>
</reportSets>
<configuration>
<aggregate>true</aggregate>
</configuration>
</plugin>
The commandline run was mvn clean site:stage
Edit: Per the comments, there is a codehaus jxr plugin with slightly different semantics. Be sure to use the org.apache.maven.plugins version rather than the org.codehaus.mojo version.