views:

43

answers:

2

Hi, I am new to Maven and trying to accomplish a simple task:

  1. build jar package and web site [DONE]
  2. deploy them to remote server via scp [DONE]
  3. 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:

  1. put <item name="Downloads" href="download.html"/> into the site.xml
  2. mvn commons:download-page
  3. 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.

A: 

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

Pascal Thivent
Thanks for explanation and a simple solution.
mumas
+1  A: 

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&lt;/linkpage.repobaseurl&gt;

    <!-- 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 += """
<a href="${path}">${it}</a><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>
seanizer
LOL, more gmaven madness ;) +1
Pascal Thivent
Yup, GMaven's so much fun, I am using it as a giant hammer and looking for nails everywhere I can :-) . No seriously, it's so much simpler than maven plugin writing while offering the same functionality. And direct access to the plexus container using `session.lookup()` is extremely powerful.
seanizer