Fork me on GitHub

Using with Maven Invoker and Repositories

The aim here is to combine the usage of the mrm-maven-plugin with the maven-invoker-plugin to allow the integration tests of a Maven Plugin to be run against a test local repository thereby ensuring that the plugin does not get installed into the local repository until after the integration tests have been confirmed as passing.

First we need to modify the pom.xml to reference both mrm-maven-plugin and the maven-invoker-plugin

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-invoker-plugin</artifactId>
        <version>2.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>install</goal>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
          <projectsDirectory>src/it/projects</projectsDirectory>
          <settingsFile>src/it/mrm/settings.xml</settingsFile>
          <filterProperties>
            <mrm.repository.url>${mrm.repository.url}</mrm.repository.url>
          </filterProperties>
          ...
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>mrm-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <goals>
              <goal>start</goal>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <repositories>

            <!-- serve content from mock repository -->
            <mockRepo>
              <source>src/it/mrm/repository</source>
              <!-- cloneTo>target/mock-repo</cloneTo -->
              <!-- cloneClean>true</cloneClean -->
              <!-- lazyArchiver>false</lazyArchiver -->
            </mockRepo>

            <!-- serve content installed by maven-invoker-plugin -->
            <localRepo>
              <source>${project.build.directory}/local-repo</source>
            </localRepo>

            <!-- pass everything else to current Maven instance -->
            <proxyRepo/>
          </repositories>
        </configuration>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

This will result in the following mojos being executed during the lifecycle as follows:

Phase Goal(s)
pre-integration-test mrm:start, invoker:install
integration-test invoker:integration-test
post-integration-test mrm:stop
verify invoker:verify

Additionally, when mrm:start is invoked, it will bind to an available port and set the property repository.proxy.url to the URL on which the repository is being hosted, and the repository content will be based on the mockRepo content generated from the following files in src/it/mrm/repository:

  • **/*.pom
  • **/*-\{classifier\}.{type} as file, which will use the GAV of the corresponding pom -file (e.g. mojo-parent-10.pom with mojo-parent-10-site.xml )
  • **/*.jar as directory, which will use the GAV of the corresponding pom -file. MRM will create an archive of this directory.
  • **/*-\{classifier\}.jar as directory, which will use the GAV of the corresponding pom -file. MRM will create an archive of this directory.
  • archetype-catalog.xml

In additional content install by maven-invoker-plugin:install will be available by localRepo

Finally thanks to proxyRepo, all the repositories available to the current Maven instance will be available too.

We tell maven-invoker-plugin to use the settings.xml file from src/it/mrm/settings.xml which will look something like:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <mirrors>
    <mirror>
      <id>mrm-maven-plugin</id>
      <name>Mock Repository Manager</name>
      <url>@mrm.repository.url@</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>it-repo</id>
      <repositories>
        <repository>
          <id>snapshots</id>
          <url>@mrm.repository.url@</url>
          <releases>
            <enabled>true</enabled>
            <checksumPolicy>ignore</checksumPolicy>
            <updatePolicy>never</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <checksumPolicy>ignore</checksumPolicy>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>snapshots</id>
          <url>@mrm.repository.url@</url>
          <releases>
            <enabled>true</enabled>
            <checksumPolicy>ignore</checksumPolicy>
            <updatePolicy>never</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <checksumPolicy>ignore</checksumPolicy>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>it-repo</activeProfile>
  </activeProfiles>
  
</settings>

And we also have instructed the maven-invoker-plugin to filter in the property value repository.proxy.url Thus when the integration tests run, they will use the mock repository manager that we have provisioned.