You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Dominic Mitchell <do...@semantico.com> on 2008/09/29 14:04:01 UTC

Accessing project version number from Java

What's the easiest way to get at the version number in the POM from  
Java?

I've been using getResourceAsStream('META-INF/maven/groupId/artifactId/ 
pom.properties');  But this doesn't work while I'm developing in  
Eclipse (only when using the built jar file).

Ideally, I'd like to somehow generate a Java class which contains the  
version number.  I thought I should be able to do this using resource  
filtering, but I couldn't see a way to configure the resources plugin  
to achieve this…

Any help would be most gratefully appreciated.

Thanks,
-Dom

Re: Accessing project version number from Java

Posted by Lee Meador <le...@leemeador.com>.
I use this little snippet inside ant build.xml target named
versionProperties to generate a Java file that gets compiled and provides a
constant (as ProgramVersion.VERSION):

        <property name="generated.src.dir"
location="${basedir}/target/generated-sources/java" />
        <echo
file="${generated.src.dir}/com/company/version/ProgramVersion.java">
            package com.company.version;
            /** Generated in ANT */
            public class ProgramVersion
            {
               public static final String VERSION = "${program.version}";
            }
        </echo>

Then this goes inside the <build><plugins> section of the pom:

<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <property name="program.version"
value="${pom.version}"></property>
                                <ant antfile="build.xml" dir="."
target="versionProperties" inheritRefs="true" />
                            </tasks>
                            <sourceRoot>

${project.build.directory}/generated-sources/java
                            </sourceRoot>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

No need to use the pom.version value to set it but you do have to not put a
double-quote or a newline in there.

It works.

-- Lee

On Mon, Sep 29, 2008 at 7:37 AM, Dominic Mitchell <do...@semantico.com> wrote:

> On 29 Sep 2008, at 13:22, Mark Hobson wrote:
>
>  2008/9/29 Dominic Mitchell <do...@semantico.com>:
>>
>>> What's the easiest way to get at the version number in the POM from Java?
>>>
>>> I've been using
>>> getResourceAsStream('META-INF/maven/groupId/artifactId/pom.properties');
>>> But this doesn't work while I'm developing in Eclipse (only when using
>>> the
>>> built jar file).
>>>
>>
>> You can use maven-runtime to dynamically obtain this information, but
>> it still has the limitation of requiring the META-INF files created at
>> package-time:
>>
>> http://maven.apache.org/shared/maven-runtime/
>>
>>
> Aha!  Thanks for pointing this out.  That's much more useful than rolling
> my own implementation of such a thing.
>
>  I'd quite like to see the generation of these metadata files occur at
>> the generate-resources phase of the lifecycle which would allow it
>> work within the IDE too.
>>
>
> -Dom
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>


-- 
-- Lee Meador
Sent from gmail. My real email address is lee AT leemeador.com

Re: Accessing project version number from Java

Posted by Dominic Mitchell <do...@semantico.com>.
On 29 Sep 2008, at 13:22, Mark Hobson wrote:

> 2008/9/29 Dominic Mitchell <do...@semantico.com>:
>> What's the easiest way to get at the version number in the POM from  
>> Java?
>>
>> I've been using
>> getResourceAsStream('META-INF/maven/groupId/artifactId/ 
>> pom.properties');
>> But this doesn't work while I'm developing in Eclipse (only when  
>> using the
>> built jar file).
>
> You can use maven-runtime to dynamically obtain this information, but
> it still has the limitation of requiring the META-INF files created at
> package-time:
>
> http://maven.apache.org/shared/maven-runtime/
>

Aha!  Thanks for pointing this out.  That's much more useful than  
rolling my own implementation of such a thing.

> I'd quite like to see the generation of these metadata files occur at
> the generate-resources phase of the lifecycle which would allow it
> work within the IDE too.

-Dom

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


Re: Accessing project version number from Java

Posted by Mark Hobson <ma...@gmail.com>.
2008/9/29 Dominic Mitchell <do...@semantico.com>:
> What's the easiest way to get at the version number in the POM from Java?
>
> I've been using
> getResourceAsStream('META-INF/maven/groupId/artifactId/pom.properties');
>  But this doesn't work while I'm developing in Eclipse (only when using the
> built jar file).
>
> Ideally, I'd like to somehow generate a Java class which contains the
> version number.  I thought I should be able to do this using resource
> filtering, but I couldn't see a way to configure the resources plugin to
> achieve this…
>
> Any help would be most gratefully appreciated.

You can use maven-runtime to dynamically obtain this information, but
it still has the limitation of requiring the META-INF files created at
package-time:

http://maven.apache.org/shared/maven-runtime/

I'd quite like to see the generation of these metadata files occur at
the generate-resources phase of the lifecycle which would allow it
work within the IDE too.

Cheers,

Mark

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


Re: Accessing project version number from Java

Posted by Dominic Mitchell <do...@semantico.com>.
On 29 Sep 2008, at 13:08, Brett Porter wrote:

> 2008/9/29 Dominic Mitchell <do...@semantico.com>:
>> What's the easiest way to get at the version number in the POM from  
>> Java?
>>
>> I've been using
>> getResourceAsStream('META-INF/maven/groupId/artifactId/ 
>> pom.properties');
>> But this doesn't work while I'm developing in Eclipse (only when  
>> using the
>> built jar file).
>
> Yes, this is only generated when the JAR is packaged. It might be best
> for your test cases to substitute in a known value here rather than
> relying on it, unless you are doing something specific with the
> version.

There's nothing particularly important about this — it's just a minor  
annoyance.  We already substitute "[dev]" if we can't read it.

>> Ideally, I'd like to somehow generate a Java class which contains the
>> version number.  I thought I should be able to do this using resource
>> filtering, but I couldn't see a way to configure the resources  
>> plugin to
>> achieve this…
>
> @project.version@ should be substituted in a copy, but resources are
> processed after compilation so the timing is not correct. You'd need
> to execute a specific plugin in the generate-sources phase.


I'll have a play with this to see if I can get it to do what I want.

Thanks for your quick response!
-Dom
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Accessing project version number from Java

Posted by Brett Porter <br...@gmail.com>.
2008/9/29 Dominic Mitchell <do...@semantico.com>:
> What's the easiest way to get at the version number in the POM from Java?
>
> I've been using
> getResourceAsStream('META-INF/maven/groupId/artifactId/pom.properties');
>  But this doesn't work while I'm developing in Eclipse (only when using the
> built jar file).

Yes, this is only generated when the JAR is packaged. It might be best
for your test cases to substitute in a known value here rather than
relying on it, unless you are doing something specific with the
version.

>
> Ideally, I'd like to somehow generate a Java class which contains the
> version number.  I thought I should be able to do this using resource
> filtering, but I couldn't see a way to configure the resources plugin to
> achieve this…

@project.version@ should be substituted in a copy, but resources are
processed after compilation so the timing is not correct. You'd need
to execute a specific plugin in the generate-sources phase.

- Brett

>
> Any help would be most gratefully appreciated.
>
> Thanks,
> -Dom



-- 
Brett Porter
Blog: http://blogs.exist.com/bporter/

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