Hi Francois,

Francois Fernandes wrote:
> for some time now I've been looking for a easy way to generate a simple
> class containing version information of our artifact. I know that it is
> possible to filter resources. But to avoid any resource loading issues I
> would like maven to generate such a class.
> Does anyone have a idea how to solve this? I'm sure that using resource
> filtering of a specific class template and then attaching the generated
> source(-folder) using the build-helper-maven-plugin, but is this a
> elegant way?

I needed this for a project of mine, and ended up using
maven-antrun-plugin to do the heavy lifting.

<aside>
BTW, in other projects I have used the Maven-populated
META-INF/org/example/pkg/pom.properties to do version discovery.  Note
that pom.properties is scoped by the project's groupId, so if you have
sufficiently distinguishable groupIds, you don't have to worry about
getting the wrong information.
</aside>

Here's a (simplified) excerpt from the POM that generates a Version
class - the antrun plugin instructs Maven to include the generated
source using the <sourceRoot> tag in its <configuration>:

<build>
  ...
  <plugins>
    ...
    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <id>iterpolate-Version.java</id>
          <phase>generate-sources</phase>
          <goals><goal>run</goal></goals>
          <configuration>
            <tasks>
              <mkdir dir="${generated-java-dir}/org/cnlp/tt" />
              <copy file="src/main/java-templates/Version.java"
                    todir="${generated-java-dir}/org/example/pkg">
                <filterset>
                  <filter token="VERSION"
                          value="${project.version}" />
                </filterset>
              </copy>
            </tasks>
            <sourceRoot>
              target/generated-sources/main/java
            </sourceRoot>
          </configuration>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
  ...
</build>
...
<properties>
  <generated-java-dir>
    target/generated-sources/main/java
  </generated-java-dir>
  ...
</properties>

And here is src/main/java-templates/Version.java:

  package org.example.pkg;

  public class Version {
    private static final String VERSION = "@VERSION@";
    public static String getVersion() { return VERSION; }
    private TTVersion() {}
  }

Hope it helps,
Steve

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

Reply via email to