You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Mark Chaimungkalanont <ma...@atlassian.com> on 2006/05/23 05:23:01 UTC

[m2] Getting SNAPSHOT information into a webapp

Guys,

We're using Maven2 and wanted to know the best way to get version information (including 
the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a webapp that was built 
with the "mvn package"?

My guess is that there is a property ${maven.snapshot.version} or something that we can 
use to generate a properties file so that the app can read this information. Perhaps a 
filter copy plugin against one of the goals?

Does anything know any references around this area? Have anyone got examples they can share?

Thanks,

Mark C

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: [m2] Getting SNAPSHOT information into a webapp

Posted by Steve Shucker <ss...@vmsinfo.com>.
Here's the source for my homebrew plugin to add a version file to my 
artifacts.  It's just writing a date and hostname, but you can easily 
change it to include other information such as version.

package com.vms.maven;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;


/**
 * Goal which creates/updates a version file
 *
 * @goal versionPage
 * @phase generate-resources
 */
public class VersionMojo extends AbstractMojo {
   
    /**
     * @parameter default-value="version.html"
     */
    private String filename;

    /**
     * @parameter default-value="MM/dd/yyyy HH:mm:ss"
     */
    private String dateFormat;

    /**
     * @parameter expression="${project}"
     * @required @readonly
     */
    protected MavenProject project;
       
    public void execute() throws MojoExecutionException {
        String outputDirectory;
        if ("jar".equals(project.getPackaging())) {
            outputDirectory = project.getBuild().getDirectory() + 
"/classes";
        } else {
            outputDirectory = project.getBuild().getDirectory() + "/" + 
project.getBuild().getFinalName();
        }
        File directory = new File(outputDirectory);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        String fullFilename = outputDirectory + "/" + filename;
        File versionFile = new File(fullFilename);
        if (versionFile.exists()) {
            versionFile.delete();
        }
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            String versionString = "built " + formatter.format(new 
Date()) + " on " + InetAddress.getLocalHost().getHostName();
            FileOutputStream output = new FileOutputStream(fullFilename);
            new PrintStream(output).println(versionString);
            output.close();
            getLog().info("Wrote version \"" + versionString + "\" to 
file " + fullFilename);
        } catch (Exception e) {
            throw new MojoExecutionException("FAILURE", e);
        }
    }
}

-Steve

hdockter wrote:
> Hi Mark, 
>
> I'm having the same requirement. I'm wondering if you have found a solution
> on how to access the snapshot timestamp value.
>
> Thanks,
>
> - Hans 
>
>
> Mark Chaimungkalanont wrote:
>   
>> Guys,
>>
>> We're using Maven2 and wanted to know the best way to get version
>> information (including 
>> the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a webapp
>> that was built 
>> with the "mvn package"?
>>
>> My guess is that there is a property ${maven.snapshot.version} or
>> something that we can 
>> use to generate a properties file so that the app can read this
>> information. Perhaps a 
>> filter copy plugin against one of the goals?
>>
>> Does anything know any references around this area? Have anyone got
>> examples they can share?
>>
>> Thanks,
>>
>> Mark C
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>>
>>
>>     
>
>   

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: [m2] Getting SNAPSHOT information into a webapp

Posted by hdockter <st...@dockter.de>.
Hi Mark, 

I'm having the same requirement. I'm wondering if you have found a solution
on how to access the snapshot timestamp value.

Thanks,

- Hans 


Mark Chaimungkalanont wrote:
> 
> Guys,
> 
> We're using Maven2 and wanted to know the best way to get version
> information (including 
> the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a webapp
> that was built 
> with the "mvn package"?
> 
> My guess is that there is a property ${maven.snapshot.version} or
> something that we can 
> use to generate a properties file so that the app can read this
> information. Perhaps a 
> filter copy plugin against one of the goals?
> 
> Does anything know any references around this area? Have anyone got
> examples they can share?
> 
> Thanks,
> 
> Mark C
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/-m2--Getting-SNAPSHOT-information-into-a-webapp-tf1666684s177.html#a8940653
Sent from the Maven - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Shared library to process resources [was RE : [m2] Getting SNAPSHOT information into a webapp]

Posted by Olivier Lamy <ol...@accor.com>.
Hi,
Perso, I think It should be well about having something like a
maven-shared-resources in this
http://svn.apache.org/viewvc/maven/shared/trunk/.
A simple library to process resources with filtering (from file and
interpolation from the pom).
This could be used in few plugins :
- maven-resources-plugin
- maven-war-plugin (which as I see contains duplicate code from
maven-resources-plugin)
- in my company plugin (which contains duplicate code too ;-))
- in some others companies plugins (which contains duplicate code too
duplicate smiley ;-)

WDYT ?

--
Olivier

-----Message d'origine-----
De : Wayne Fay [mailto:waynefay@gmail.com] 
Envoyé : mardi 23 mai 2006 17:02
À : Maven Users List
Objet : Re: [m2] Getting SNAPSHOT information into a webapp


You might need to just write your own plug-in to get this kind of
properties file output.

Wayne

On 5/23/06, Mark Chaimungkalanont <ma...@atlassian.com> wrote:
> Thanks Kenney. I think what I want is similar to how the 
> pom.properties would normally be generated by Maven. As in, I want 
> some code / cofiguration that can generate a properties file with the 
> "version" in it.
>
> For example, I want something to work like
>
> eg.
> myApp.properties.template (with contents) 
> version.from.maven=${maven.pom.version}-${maven.package.build.date}
>
> to produce:
>
> myApp.properties (with contents)
>
> version.from.maven=1.0-SNAPSHOT-2006-05-22
>
> And so I can place in the myApp.properties in the class path and 
> easily read it?
> -------------------------------------
> ATLASSIAN - http://www.atlassian.com
> Australia's Fastest Growing Software Company 2002-05 [BRW Magazine]
>
> Kenney Westerhof wrote:
> > On Tue, 23 May 2006, Mark Chaimungkalanont wrote:
> >
> > Maven writes a property file in the jar or war at 
> > /META-INF/maven/<groupId>/<artifactId>/pom.properties.
> >
> > For jars you can use a classloader to find that resource, but in the

> > case of a WAR the META-INF is not part of the classpath so you'd 
> > have to use the servlet api to get the path to that file.
> >
> > Code snippet:
> >
> >     public static String getVersion( String groupId, String
artifactId )
> >         throws IOException
> >     {
> >         ClassLoader cl =
Thread.currentThread().getContextClassLoader();
> >         Properties props = new Properties();
> >
> >         String propFileName = "META-INF/maven/" + groupId.replace( 
> > '.', '/' ) + "/" + artifactId + "/pom.properties";
> >         InputStream a = cl.getResourceAsStream( propFileName );
> >         if ( a == null )
> >             throw new IOException( "Cannot find '" + propFileName +
"'" );
> >         props.load( a );
> >         return props.getProperty( "version" );
> >     }
> >
> >
> > -- Kenney
> >
> >
> >> Guys,
> >>
> >> We're using Maven2 and wanted to know the best way to get version 
> >> information (including the SNAPSHOT timestamp, e.g. 
> >> 1-0-SNAPSHOT-20050622 or sth) into a webapp that was built with the

> >> "mvn package"?
> >>
> >> My guess is that there is a property ${maven.snapshot.version} or 
> >> something that we can use to generate a properties file so that the

> >> app can read this information. Perhaps a filter copy plugin against

> >> one of the goals?
> >>
> >> Does anything know any references around this area? Have anyone got

> >> examples they can share?
> >>
> >> Thanks,
> >>
> >> Mark C
> >>
> >> -------------------------------------------------------------------
> >> --
> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> For additional commands, e-mail: users-help@maven.apache.org
> >>
> >
> > --
> > Kenney Westerhof
> > http://www.neonics.com
> > GPG public key: http://www.gods.nl/~forge/kenneyw.key
> >
> > --------------------------------------------------------------------
> > -
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org



This e-mail, any attachments and the information contained therein ("this message") are confidential and intended solely for the use of the addressee(s). If you have received this message in error please send it back to the sender and delete it. Unauthorized publication, use, dissemination or disclosure of this message, either in whole or in part is strictly prohibited.
********************************************************************** 
Ce message électronique et tous les fichiers joints ainsi que  les informations contenues dans ce message ( ci après "le message" ), sont confidentiels et destinés exclusivement à l'usage de la  personne à laquelle ils sont adressés. Si vous avez reçu ce message par erreur, merci  de le renvoyer à son émetteur et de le détruire. Toutes diffusion, publication, totale ou partielle ou divulgation sous quelque forme que se soit non expressément autorisées de ce message, sont interdites.
********************************************************************** 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Fwd: [m2] Getting SNAPSHOT information into a webapp

Posted by Gautham Pamu <ga...@gmail.com>.
---------- Forwarded message ----------
From: Wayne Fay <wa...@gmail.com>
Date: May 23, 2006 10:02 AM
Subject: Re: [m2] Getting SNAPSHOT information into a webapp
To: Maven Users List <us...@maven.apache.org>

You might need to just write your own plug-in to get this kind of
properties file output.

Wayne

On 5/23/06, Mark Chaimungkalanont <ma...@atlassian.com> wrote:
> Thanks Kenney. I think what I want is similar to how the pom.propertieswould normally be
> generated by Maven. As in, I want some code / cofiguration that can
generate a properties
> file with the "version" in it.
>
> For example, I want something to work like
>
> eg.
> myApp.properties.template (with contents)
> version.from.maven=${maven.pom.version}-${maven.package.build.date}
>
> to produce:
>
> myApp.properties (with contents)
>
> version.from.maven=1.0-SNAPSHOT-2006-05-22
>
> And so I can place in the myApp.properties in the class path and easily
read it?
> -------------------------------------
> ATLASSIAN - http://www.atlassian.com
> Australia's Fastest Growing Software Company 2002-05 [BRW Magazine]
>
> Kenney Westerhof wrote:
> > On Tue, 23 May 2006, Mark Chaimungkalanont wrote:
> >
> > Maven writes a property file in the jar or war at
> > /META-INF/maven/<groupId>/<artifactId>/pom.properties.
> >
> > For jars you can use a classloader to find that resource, but in the
case
> > of a WAR the META-INF is not part of the classpath so you'd have to use
> > the servlet api to get the path to that file.
> >
> > Code snippet:
> >
> >     public static String getVersion( String groupId, String artifactId )
> >         throws IOException
> >     {
> >         ClassLoader cl = Thread.currentThread().getContextClassLoader();
> >         Properties props = new Properties();
> >
> >         String propFileName = "META-INF/maven/" + groupId.replace( '.',
> > '/' ) + "/" + artifactId + "/pom.properties";
> >         InputStream a = cl.getResourceAsStream( propFileName );
> >         if ( a == null )
> >             throw new IOException( "Cannot find '" + propFileName + "'"
);
> >         props.load( a );
> >         return props.getProperty( "version" );
> >     }
> >
> >
> > -- Kenney
> >
> >
> >> Guys,
> >>
> >> We're using Maven2 and wanted to know the best way to get version
information (including
> >> the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a
webapp that was built
> >> with the "mvn package"?
> >>
> >> My guess is that there is a property ${maven.snapshot.version} or
something that we can
> >> use to generate a properties file so that the app can read this
information. Perhaps a
> >> filter copy plugin against one of the goals?
> >>
> >> Does anything know any references around this area? Have anyone got
examples they can share?
> >>
> >> Thanks,
> >>
> >> Mark C
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> For additional commands, e-mail: users-help@maven.apache.org
> >>
> >
> > --
> > Kenney Westerhof
> > http://www.neonics.com
> > GPG public key: http://www.gods.nl/~forge/kenneyw.key
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org



-- 
-Gautham Pamu

Re: [m2] Getting SNAPSHOT information into a webapp

Posted by Wayne Fay <wa...@gmail.com>.
You might need to just write your own plug-in to get this kind of
properties file output.

Wayne

On 5/23/06, Mark Chaimungkalanont <ma...@atlassian.com> wrote:
> Thanks Kenney. I think what I want is similar to how the pom.properties would normally be
> generated by Maven. As in, I want some code / cofiguration that can generate a properties
> file with the "version" in it.
>
> For example, I want something to work like
>
> eg.
> myApp.properties.template (with contents)
> version.from.maven=${maven.pom.version}-${maven.package.build.date}
>
> to produce:
>
> myApp.properties (with contents)
>
> version.from.maven=1.0-SNAPSHOT-2006-05-22
>
> And so I can place in the myApp.properties in the class path and easily read it?
> -------------------------------------
> ATLASSIAN - http://www.atlassian.com
> Australia's Fastest Growing Software Company 2002-05 [BRW Magazine]
>
> Kenney Westerhof wrote:
> > On Tue, 23 May 2006, Mark Chaimungkalanont wrote:
> >
> > Maven writes a property file in the jar or war at
> > /META-INF/maven/<groupId>/<artifactId>/pom.properties.
> >
> > For jars you can use a classloader to find that resource, but in the case
> > of a WAR the META-INF is not part of the classpath so you'd have to use
> > the servlet api to get the path to that file.
> >
> > Code snippet:
> >
> >     public static String getVersion( String groupId, String artifactId )
> >         throws IOException
> >     {
> >         ClassLoader cl = Thread.currentThread().getContextClassLoader();
> >         Properties props = new Properties();
> >
> >         String propFileName = "META-INF/maven/" + groupId.replace( '.',
> > '/' ) + "/" + artifactId + "/pom.properties";
> >         InputStream a = cl.getResourceAsStream( propFileName );
> >         if ( a == null )
> >             throw new IOException( "Cannot find '" + propFileName + "'" );
> >         props.load( a );
> >         return props.getProperty( "version" );
> >     }
> >
> >
> > -- Kenney
> >
> >
> >> Guys,
> >>
> >> We're using Maven2 and wanted to know the best way to get version information (including
> >> the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a webapp that was built
> >> with the "mvn package"?
> >>
> >> My guess is that there is a property ${maven.snapshot.version} or something that we can
> >> use to generate a properties file so that the app can read this information. Perhaps a
> >> filter copy plugin against one of the goals?
> >>
> >> Does anything know any references around this area? Have anyone got examples they can share?
> >>
> >> Thanks,
> >>
> >> Mark C
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> For additional commands, e-mail: users-help@maven.apache.org
> >>
> >
> > --
> > Kenney Westerhof
> > http://www.neonics.com
> > GPG public key: http://www.gods.nl/~forge/kenneyw.key
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: [m2] Getting SNAPSHOT information into a webapp

Posted by Mark Chaimungkalanont <ma...@atlassian.com>.
Thanks Kenney. I think what I want is similar to how the pom.properties would normally be 
generated by Maven. As in, I want some code / cofiguration that can generate a properties 
file with the "version" in it.

For example, I want something to work like

eg.
myApp.properties.template (with contents)
version.from.maven=${maven.pom.version}-${maven.package.build.date}

to produce:

myApp.properties (with contents)

version.from.maven=1.0-SNAPSHOT-2006-05-22

And so I can place in the myApp.properties in the class path and easily read it?
-------------------------------------
ATLASSIAN - http://www.atlassian.com
Australia's Fastest Growing Software Company 2002-05 [BRW Magazine]

Kenney Westerhof wrote:
> On Tue, 23 May 2006, Mark Chaimungkalanont wrote:
> 
> Maven writes a property file in the jar or war at
> /META-INF/maven/<groupId>/<artifactId>/pom.properties.
> 
> For jars you can use a classloader to find that resource, but in the case
> of a WAR the META-INF is not part of the classpath so you'd have to use
> the servlet api to get the path to that file.
> 
> Code snippet:
> 
>     public static String getVersion( String groupId, String artifactId )
>         throws IOException
>     {
>         ClassLoader cl = Thread.currentThread().getContextClassLoader();
>         Properties props = new Properties();
> 
>         String propFileName = "META-INF/maven/" + groupId.replace( '.',
> '/' ) + "/" + artifactId + "/pom.properties";
>         InputStream a = cl.getResourceAsStream( propFileName );
>         if ( a == null )
>             throw new IOException( "Cannot find '" + propFileName + "'" );
>         props.load( a );
>         return props.getProperty( "version" );
>     }
> 
> 
> -- Kenney
> 
> 
>> Guys,
>>
>> We're using Maven2 and wanted to know the best way to get version information (including
>> the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a webapp that was built
>> with the "mvn package"?
>>
>> My guess is that there is a property ${maven.snapshot.version} or something that we can
>> use to generate a properties file so that the app can read this information. Perhaps a
>> filter copy plugin against one of the goals?
>>
>> Does anything know any references around this area? Have anyone got examples they can share?
>>
>> Thanks,
>>
>> Mark C
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
> 
> --
> Kenney Westerhof
> http://www.neonics.com
> GPG public key: http://www.gods.nl/~forge/kenneyw.key
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: [m2] Getting SNAPSHOT information into a webapp

Posted by Kenney Westerhof <ke...@apache.org>.
On Tue, 23 May 2006, Mark Chaimungkalanont wrote:

Maven writes a property file in the jar or war at
/META-INF/maven/<groupId>/<artifactId>/pom.properties.

For jars you can use a classloader to find that resource, but in the case
of a WAR the META-INF is not part of the classpath so you'd have to use
the servlet api to get the path to that file.

Code snippet:

    public static String getVersion( String groupId, String artifactId )
        throws IOException
    {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Properties props = new Properties();

        String propFileName = "META-INF/maven/" + groupId.replace( '.',
'/' ) + "/" + artifactId + "/pom.properties";
        InputStream a = cl.getResourceAsStream( propFileName );
        if ( a == null )
            throw new IOException( "Cannot find '" + propFileName + "'" );
        props.load( a );
        return props.getProperty( "version" );
    }


-- Kenney


> Guys,
>
> We're using Maven2 and wanted to know the best way to get version information (including
> the SNAPSHOT timestamp, e.g. 1-0-SNAPSHOT-20050622 or sth) into a webapp that was built
> with the "mvn package"?
>
> My guess is that there is a property ${maven.snapshot.version} or something that we can
> use to generate a properties file so that the app can read this information. Perhaps a
> filter copy plugin against one of the goals?
>
> Does anything know any references around this area? Have anyone got examples they can share?
>
> Thanks,
>
> Mark C
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>

--
Kenney Westerhof
http://www.neonics.com
GPG public key: http://www.gods.nl/~forge/kenneyw.key

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org