Skip to content

Commit

Permalink
Improve the maven deployment of the synthetic test jars
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Sep 20, 2019
1 parent 91d1f21 commit 6277cf6
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 87 deletions.
101 changes: 95 additions & 6 deletions build/scripts/generate-test-poms.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,25 @@
*/

import groovy.io.FileType
import static org.twdata.maven.mojoexecutor.MojoExecutor.Element
import static org.twdata.maven.mojoexecutor.MojoExecutor.*
import org.apache.maven.project.MavenProject
import org.apache.maven.execution.MavenSession
import org.apache.maven.model.Plugin

new File(project.basedir, 'target').mkdirs()
final String command = properties['itest.jar.command']

project.basedir.eachFile FileType.DIRECTORIES, { dir ->
final File pomXml = new File(dir, 'pom.xml')
if (pomXml.exists()) {
def pomXmlProject = new XmlParser().parseText(pomXml.getText('UTF-8'))
static boolean isItestModule(MavenProject project) {
return 'jar'.equals(project.packaging) && new File(project.basedir, 'pom.xml').getText('UTF-8').contains('<goal>native-image</goal>')
}

static void install(MavenProject project, MavenSession session) {

if (isItestModule(project)) {
def pomXmlProject = new XmlParser().parseText(new File(project.basedir, 'pom.xml').getText('UTF-8'))
final String oldArtifactId = pomXmlProject.artifactId.text()
final String newArtifactId = oldArtifactId + '-tests'

pomXmlProject.artifactId[0].value = newArtifactId

pomXmlProject.name.each { n -> n.value = n.text() + ' :: Tests' }
Expand All @@ -82,10 +92,89 @@ project.basedir.eachFile FileType.DIRECTORIES, { dir ->
pomXmlProject.build.each { n -> n.parent().remove(n) }
pomXmlProject.profiles.each { n -> n.parent().remove(n) }

new File(project.basedir, 'target/'+ newArtifactId + '-pom.xml').withWriter( 'UTF-8' ) { out ->
final File outputPom = newPomPath(project, project.artifactId)
outputPom.withWriter( 'UTF-8' ) { out ->
final XmlNodePrinter printer = new XmlNodePrinter(new PrintWriter(out))
printer.preserveWhitespace = true
printer.print(pomXmlProject)
}
final File testJar = testJarPath(project, project.basedir, project.artifactId)

Class bpmClass = Class.forName("org.apache.maven.plugin.BuildPluginManager");
Object buildPluginManager = session.lookup(bpmClass.getName());
executeMojo(
managedPlugin("org.apache.maven.plugins", "maven-install-plugin", project),
goal("install-file"),
configuration(
element("pomFile", outputPom.toString()),
element("version", project.version),
element("file", testJar.toString())
),
executionEnvironment(
project,
session,
buildPluginManager
)
)
}

}

static Plugin managedPlugin(String groupId, String artifactId, MavenProject project) {
for (p in project.build.plugins) {
if (groupId.equals(p.groupId) && artifactId.equals(p.artifactId)) {
println 'Resolved version ' + p.version + ' for ' + groupId +':'+artifactId
return plugin(groupId, artifactId, p.version)
}
}
throw new IllegalStateException('Could not find a managed version of '+ groupId + ':' + artifactId)
}


static File newPomPath(MavenProject project, String oldArtifactId) {
return new File(project.basedir, 'target/'+ oldArtifactId + '-tests-pom.xml')
}
static File testJarPath(MavenProject project, File oldDir, String oldArtifactId) {
return new File(oldDir, 'target/' + oldArtifactId + '-' + project.version + '-tests.jar')
}

static void deploy(MavenProject project, MavenSession session) {
if (isItestModule(project)) {
def distManagementRepo = project.version.endsWith('-SNAPSHOT') ? project.distributionManagement.snapshotRepository : project.distributionManagement.repository

final File outputPom = newPomPath(project, project.artifactId)
final File testJar = testJarPath(project, project.basedir, project.artifactId)

Class bpmClass = Class.forName("org.apache.maven.plugin.BuildPluginManager");
Object buildPluginManager = session.lookup(bpmClass.getName());

List<Element> config = [];
config.add(element("repositoryId", distManagementRepo.id))
config.add(element("url", distManagementRepo.url))
config.add(element("pomFile", outputPom.toString()))
config.add(element("version", project.version))
config.add(element("file", testJar.toString()))

final File testSources = new File(testJar.toString().replace('.jar', '-sources.jar'))
if (testSources.exists()) {
config.add(element("sources", testSources.toString()))
}
final File testJavaDoc = new File(testJar.toString().replace('.jar', '-javadoc.jar'))
if (testJavaDoc.exists()) {
config.add(element("javadoc", testJavaDoc.toString()))
}

executeMojo(
managedPlugin("org.apache.maven.plugins", "maven-deploy-plugin", project),
goal("deploy-file"),
configuration(config.toArray(new Element[config.size()])),
executionEnvironment(
project,
session,
buildPluginManager
)
)
}
}


1 change: 0 additions & 1 deletion integration-tests/install-reusable-test-jar.txt

This file was deleted.

110 changes: 30 additions & 80 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,96 +140,46 @@
<artifactId>groovy-maven-plugin</artifactId>
<executions>
<execution>
<inherited>false</inherited>
<id>generate-test-poms</id>
<id>install-test-poms</id>
<goals>
<goal>execute</goal>
</goals>
<phase>validate</phase>
<phase>install</phase>
<configuration>
<source>file:///${project.basedir}/../build/scripts/generate-test-poms.groovy</source>
<source><![CDATA[
File dir = project.basedir
for (; dir != null && !new File(dir, '.mvn').exists(); dir = dir.getParentFile()) {}
new GroovyShell().parse(new File(dir, 'build/scripts/generate-test-poms.groovy'))
.install(project, session)
]]></source>
</configuration>
</execution>
<execution>
<id>deploy-test-poms</id>
<goals>
<goal>execute</goal>
</goals>
<phase>install</phase>
<configuration>
<source><![CDATA[
File dir = project.basedir
for (; dir != null && !new File(dir, '.mvn').exists(); dir = dir.getParentFile()) {}
new GroovyShell().parse(new File(dir, 'build/scripts/generate-test-poms.groovy'))
.deploy(project, session)
]]></source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.twdata.maven</groupId>
<artifactId>mojo-executor</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>

</build>

<profiles>
<profile>
<!-- installs and deploys the test jar under a separate artifactId and with a modified pom.
See /build/scripts/generate-test-poms.groovy -->
<id>install-reusable-test-jar</id>
<activation>
<file>
<!-- activate only in the submodules -->
<exists>../install-reusable-test-jar.txt</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-reusable-test-jar</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<pomFile>${basedir}/../target/${project.artifactId}-tests-pom.xml</pomFile>
<version>${project.version}</version>
<file>${basedir}/target/${project.artifactId}-${project.version}-tests.jar</file>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<executions>
<execution>
<id>set-deployer-properties</id>
<goals>
<goal>execute</goal>
</goals>
<phase>deploy</phase>
<configuration>
<source><![CDATA[
def distManagementRepo = project.version.endsWith('-SNAPSHOT') ? project.distributionManagement.snapshotRepository : project.distributionManagement.repository
project.properties.setProperty('deploy.test.jar.repositoryId', distManagementRepo.id)
project.properties.setProperty('deploy.test.jar.url', distManagementRepo.url)
]]></source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>deploy-reusable-test-jar</id>
<goals>
<goal>deploy-file</goal>
</goals>
<phase>deploy</phase>
<configuration>
<repositoryId>${deploy.test.jar.repositoryId}</repositoryId><!-- set via groovy above -->
<url>${deploy.test.jar.url}</url><!-- set via groovy above -->
<pomFile>${basedir}/../target/${project.artifactId}-tests-pom.xml</pomFile>
<version>${project.version}</version>
<file>${basedir}/target/${project.artifactId}-${project.version}-tests.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
Expand Down

0 comments on commit 6277cf6

Please sign in to comment.