MavenSnips

From Custard
Jump to: navigation, search

Contents

Maven Snips

Referencing version numbers in pom and child poms

\
...
    <modelVersion>4.0.0</modelVersion>

    <groupId>pomegranate.project</groupId>
    <artifactId>PomegranateProject</artifactId>
    <!-- Strictly speaking Maven doesn't like this substitution, but hey! -->
    <version>${pomegranate_project_version}</version>

    <packaging>pom</packaging>

    <properties>
        <!-- Edit these two lines to change the project version / snapshot status -->
        <pomegranate_project_version_number>1.3.3</pomegranate_project_version_number>
        <pomegranate_project_version>${pomegranate_project_version_number}-SNAPSHOT</pomegranate_project_version>
        <pomegranate_project_resources>./src/main/resources</pomegranate_project_resources>
        <sonar.language>flex</sonar.language>
        <sonar.dynamicAnalysis>true</sonar.dynamicAnalysis>
        <flex.version>4.5.0.18623</flex.version>
        <flexmojos.version>4.0-beta-5</flexmojos.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <service.base.name>PomegranateProject</service.base.name>
    </properties>

...

In a child pom...

...
    <parent>
        <groupId>pomegranate.project</groupId>
        <artifactId>PomegranateProject</artifactId>
        <version>${pomegranate_project_version}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
...

Inserting version numbers into files on the fly

  • Using replaceTokens
...
    <build>
        <plugins>
            <plugin>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.6</version>
              <executions>
                <execution>
                    <phase>compile</phase>
                    <configuration>
                        <target>
                            <delete file="${pomegranate_project_resources}/some.xml" quiet="true"/>
                            <copy file="${pomegranate_project_resources}/some.tpl"
                                  tofile="${pomegranate_project_resources}/some.xml"
                                  failonerror="false">
                                <filterchain>
                                   <striplinecomments>
                                      <comment value="!"/>
                                   </striplinecomments>
                                   <replacetokens>
                                        <token key="version" value="${pomegranate_project_version}"/>
                                    </replacetokens>
                                </filterchain>
                            </copy>
                        </target>
                    </configuration>
                    <goals>
                      <goal>run</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
...

Quick start

- Assuming there is an archetype...

  • Create a new project
     mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=uk.co.app.my -DartifactId=ProjectName
  • Compile
     mvn compile
  • Test Run
     java -cp target/classes uk.co.app.my.ProjectName.App

See also

http://maven.apache.org/guides/getting-started/index.html

Personal tools