Axel Fontaine
Axel Fontaine

Reputation: 35179

Install Maven itself from Maven Central

I have a maven plugin I would like to test against different Maven versions (Ex.: 2.2.1 & 3.0.4). Ideally I don't want users running the build to have to install these exact versions manually.

Is it possible to install specific versions of Maven itself from Maven Central or some other source that would then cache them in the local Maven repo for subsequent builds?

Upvotes: 2

Views: 601

Answers (3)

Petr Kozelka
Petr Kozelka

Reputation: 8000

Maven distributions are stored in Maven Central Repository, as you can see here:

Therefore, it can be used as a normal dependency with following coordinates:

tar.gz variant:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>apache-maven</artifactId>
    <version>3.0.4</version>
    <classifier>bin</classifier>
    <type>tar.gz</type>
</dependency>

zip variant:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>apache-maven</artifactId>
    <version>3.0.4</version>
    <classifier>bin</classifier>
    <type>zip</type>
</dependency>

The rest is quite standard - you will probably use it in integration test poms, and call them with maven-invoker-plugin as recommended by @khmarbaise.

Upvotes: 2

khmarbaise
khmarbaise

Reputation: 97557

You could do a thing like the following:

   <profile>
      <id>run-its</id>
      <build>
        <!--  Download the different Maven versions -->
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0-beta-3</version>
            <executions>
              <execution>
                <id>download-maven-2.0.11</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>download-single</goal>
                </goals>
                <configuration>
                  <url>http://archive.apache.org/dist/maven/binaries/</url>
                  <fromFile>apache-maven-2.0.11-bin.tar.gz</fromFile>
                  <toDir>${project.build.directory}/maven/download/</toDir>
                </configuration>
              </execution>
              <execution>
                <id>download-maven-2.2.1</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>download-single</goal>
                </goals>
                <configuration>
                  <url>http://archive.apache.org/dist/maven/binaries/</url>
                  <fromFile>apache-maven-2.2.1-bin.tar.gz</fromFile>
                  <toDir>${project.build.directory}/maven/download/</toDir>
                </configuration>
              </execution>
              <execution>
                <id>download-maven-3.0.3</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>download-single</goal>
                </goals>
                <configuration>
                  <url>http://archive.apache.org/dist/maven/binaries/</url>
                  <fromFile>apache-maven-3.0.3-bin.tar.gz</fromFile>
                  <toDir>${project.build.directory}/maven/download/</toDir>
                </configuration>
              </execution>
            </executions>
          </plugin>


          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>truezip-maven-plugin</artifactId>
            <version>1.0-beta-4</version>
            <executions>
              <execution>
                <id>extract-maven-2.0.11</id>
                <goals>
                  <goal>copy</goal>
                </goals>
                <phase>prepare-package</phase>
                <configuration>
                  <fileset>
                    <directory>${project.build.directory}/maven/download/apache-maven-2.0.11-bin.tar.gz</directory>
                    <outputDirectory>${project.build.directory}/maven/tools/</outputDirectory>
                  </fileset>
                </configuration>
              </execution>
              <execution>
                <id>extract-maven-2.2.1</id>
                <goals>
                  <goal>copy</goal>
                </goals>
                <phase>prepare-package</phase>
                <configuration>
                  <fileset>
                    <directory>${project.build.directory}/maven/download/apache-maven-2.2.1-bin.tar.gz</directory>
                    <outputDirectory>${project.build.directory}/maven/tools/</outputDirectory>
                  </fileset>
                </configuration>
              </execution>
              <execution>
                <id>extract-maven-3.0.3</id>
                <goals>
                  <goal>copy</goal>
                </goals>
                <phase>prepare-package</phase>
                <configuration>
                  <fileset>
                    <directory>${project.build.directory}/maven/download/apache-maven-3.0.3-bin.tar.gz</directory>
                    <outputDirectory>${project.build.directory}/maven/tools/</outputDirectory>
                  </fileset>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <!--
            This is currently needed due to a bug of the truezip-plugin cause it unpacks without permission!
            see http://jira.codehaus.org/browse/MOJO-1796
           -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                  <id>chmod-files</id>
                  <phase>package</phase>
                  <goals>
                    <goal>run</goal>
                  </goals>
                  <configuration>
                    <target>
                      <chmod file="${project.build.directory}/maven/tools/apache-maven-2.0.11/bin/mvn" perm="+x"/>
                      <chmod file="${project.build.directory}/maven/tools/apache-maven-2.2.1/bin/mvn" perm="+x"/>
                      <chmod file="${project.build.directory}/maven/tools/apache-maven-3.0.3/bin/mvn" perm="+x"/>
                    </target>
                  </configuration>
                </execution>
              </executions>
          </plugin>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
              <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy</artifactId>
                <version>1.8.4</version>
              </dependency>
            </dependencies>
            <configuration>
              <debug>false</debug>
                  <!-- src/it-ip as for integration tests invoker plugin for the time of transition to maven-invoker-plugin -->
              <projectsDirectory>src/it</projectsDirectory>
              <showVersion>true</showVersion>
              <pomIncludes>
                <pomInclude>*/pom.xml</pomInclude>
              </pomIncludes>
              <preBuildHookScript>setup</preBuildHookScript>
              <postBuildHookScript>verify</postBuildHookScript>
              <settingsFile>src/it/settings.xml</settingsFile>
            </configuration>
            <executions>
              <execution>
                <id>integration-test-maven-2.0.11</id>
                <goals>
                  <goal>install</goal>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <reportsDirectory>${project.build.directory}/invoker-reports-2.0.11</reportsDirectory>
                  <localRepositoryPath>${project.build.directory}/local-repo-2.0.11</localRepositoryPath>
                  <cloneProjectsTo>${project.build.directory}/it-2.0.11</cloneProjectsTo>
                  <mavenHome>${project.build.directory}/maven/tools/apache-maven-2.0.11</mavenHome>
                  <goals>
                    <goal>clean</goal>
                    <goal>test</goal>
                  </goals>
                </configuration>
              </execution>
              <execution>
                <id>integration-test-maven-2.2.1</id>
                <goals>
                  <goal>install</goal>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <reportsDirectory>${project.build.directory}/invoker-reports-2.2.1</reportsDirectory>
                  <localRepositoryPath>${project.build.directory}/local-repo-2.2.1</localRepositoryPath>
                  <cloneProjectsTo>${project.build.directory}/it-2.2.1</cloneProjectsTo>
                  <mavenHome>${project.build.directory}/maven/tools/apache-maven-2.2.1</mavenHome>
                  <goals>
                    <goal>clean</goal>
                    <goal>test</goal>
                  </goals>
                </configuration>
              </execution>
              <execution>
                <id>integration-test-maven-3.0.3</id>
                <goals>
                  <goal>install</goal>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <reportsDirectory>${project.build.directory}/invoker-reports-3.0.3</reportsDirectory>
                  <localRepositoryPath>${project.build.directory}/local-repo-3.0.3</localRepositoryPath>
                  <cloneProjectsTo>${project.build.directory}/it-3.0.3</cloneProjectsTo>
                  <mavenHome>${project.build.directory}/maven/tools/apache-maven-3.0.3</mavenHome>
                  <goals>
                    <goal>clean</goal>
                    <goal>test</goal>
                  </goals>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

This will download the different Maven version unpack the .tar.gz archives and make mvn executable and use maven-invoker-plugin to run all integration test with these different maven versions.

BUT i can't recommend that. The better way is to use a CI solution (as already mentioned) which contains the different installations of Maven. Than you can run the integration tests for each Maven version separately.

Upvotes: 0

carlspring
carlspring

Reputation: 32717

Why don't you simply just install a Continuous Integration (CI) server such as Jenkins / Hudson / TeamCity / etc? CI servers allow you to run your build against different versions of an SDK.

If your plugin is OSS (and on GitHub), I believe you can get free Jenkins hosting from Cloudbees.

Downloading Maven itself from Maven Central is not possible. You can only download it from their site.

Upvotes: 1

Related Questions