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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>

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

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to