Skip to content

Commit

Permalink
[JENKINS-51046] mvn incrementals:incrementalify
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed May 10, 2018
1 parent 4f563a8 commit 6c44568
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 9 deletions.
54 changes: 45 additions & 9 deletions README.md
Expand Up @@ -7,7 +7,39 @@ See [JEP-305](https://github.com/jenkinsci/jep/blob/master/jep/305/README.adoc)
Since most Jenkins repositories host plugins, this use case will be documented first.
You must be using parent POM version [3.9](https://github.com/jenkinsci/plugin-pom/blob/840ac3020a0a243dd243ed7156a22dae1e3c35fe/CHANGELOG.md#39) or later.

### Enabling consumption of incrementals
### Enabling incrementals (the easy way)

Just run

```bash
mvn io.jenkins.tools.incrementals:incrementals-maven-plugin:incrementalify -DgenerateBackupPoms=false
```

or if your POM is already new enough (3.10+)

```bash
mvn incrementals:incrementalify
```

Check the usual build

```bash
mvn clean package
```

and if all is well,

```bash
git add .mvn pom.xml
git checkout -b incrementals
git commit -m Incrementalified.
```

and file as a pull request.

### Enabling incrementals (the hard way)

#### Enabling consumption of incrementals

If your plugin has (or may have) dependencies on incremental versions, run:

Expand All @@ -21,7 +53,7 @@ git add .mvn

This profile merely activates access to the [Incrementals repository](https://repo.jenkins-ci.org/incrementals/).

### Enabling production of incrementals
#### Enabling production of incrementals

To produce incremental artifacts _from_ your plugin, first edit your `pom.xml`.
If your plugin declares
Expand Down Expand Up @@ -95,7 +127,17 @@ git add .mvn .gitignore pom.xml

and commit and push your edits.

If this becomes the head of a pull request built on ci.jenkins.io,
#### Production _and_ consumption

A single plugin may both consume Incrementals releases, and produce its own.
Just make both kinds of edits.
(`.mvn/maven.config` may have multiple lines.)

### Producing incrementals

Assumes you have set up the `might-produce-incrementals` as above, either by hand or using the `incrementalify` goal.

If you file a pull request built on ci.jenkins.io,
and the pull request is up to date with its target branch,
and the build is stable,
the artifact will be automatically deployed to the Incrementals repository.
Expand All @@ -112,12 +154,6 @@ mvn -Dset.changelist -DskipTests clean install
If you do not select the `-Dset.changelist` option, you will create a regular `*-SNAPSHOT` artifact.
(And that is what you _must_ do if you have any local modifications or untracked files.)

### Production _and_ consumption

A single plugin may both consume Incrementals releases, and produce its own.
Just make both kinds of edits.
(`.mvn/maven.config` may have multiple lines.)

### Updating dependencies

Once you have some dependencies on incremental versions in your POM, you can
Expand Down
@@ -0,0 +1,155 @@
/*
* The MIT License
*
* Copyright 2018 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.jenkins.tools.incrementals.maven;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.codehaus.mojo.versions.AbstractVersionsUpdaterMojo;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
* Sets a project up for Incrementals.
*/
@Mojo(name = "incrementalify", requiresDirectInvocation = true, aggregator = true)
public class IncrementalifyMojo extends AbstractVersionsUpdaterMojo {

private static final String MINIMUM_PARENT = "3.9"; // TODO 3.10 after https://github.com/jenkinsci/plugin-pom/pull/105

@Component
private BuildPluginManager pluginManager;

@Override public void execute() throws MojoExecutionException, MojoFailureException {
File dotMvn = new File(project.getBasedir(), ".mvn");
File extensionsXml = new File(dotMvn, "extensions.xml");
if (extensionsXml.isFile()) {
throw new MojoFailureException("Editing an existing " + extensionsXml + " is not yet supported");
}
VersionRange any;
ArtifactVersions gclmeVersions;
try {
any = VersionRange.createFromVersionSpec("[0,)");
gclmeVersions = getHelper().lookupArtifactVersions(getHelper().createDependencyArtifact("io.jenkins.tools.incrementals", "git-changelist-maven-extension", any, "type", null, null), true);
} catch (ArtifactMetadataRetrievalException | InvalidVersionSpecificationException x) {
throw new MojoExecutionException(x.getMessage(), x);
}
ArtifactVersion gclmeNewestVersion = gclmeVersions.getNewestVersion(any, false);
super.execute();
project.getProperties().setProperty("dollar", "$");
executeMojo(plugin("org.codehaus.mojo", "versions-maven-plugin", "2.5"), "set",
configuration(
element("newVersion", "${dollar}{revision}${dollar}{changelist}"),
element("generateBackupPoms", "false")),
executionEnvironment(project, session, pluginManager));
File mavenConfig = new File(dotMvn, "maven.config");
try {
String existing = mavenConfig.isFile() ? FileUtils.readFileToString(mavenConfig, "UTF-8") : "";
dotMvn.mkdirs();
FileUtils.writeStringToFile(mavenConfig, "-Pconsume-incrementals\n-Pmight-produce-incrementals\n" + existing, "UTF-8");
try (InputStream is = IncrementalifyMojo.class.getResourceAsStream("prototype-extensions.xml")) {
FileUtils.writeStringToFile(extensionsXml, IOUtils.toString(is).replace("@VERSION@", gclmeNewestVersion.toString()));
}
} catch (IOException x) {
throw new MojoExecutionException("failed to update " + dotMvn, x);
}
}

@Override protected void update(ModifiedPomXMLEventReader pom) throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException {
String version = PomHelper.getProjectVersion(pom);
Matcher m = Pattern.compile("(.+)-SNAPSHOT").matcher(version);
if (!m.matches()) {
throw new MojoFailureException("Unexpected version: " + version);
}
String origTag = project.getScm().getTag();
if (!origTag.equals("HEAD")) {
throw new MojoFailureException("Unexpected tag: " + origTag);
}
Artifact parent = PomHelper.getProjectParent(pom, getHelper());
if (parent == null) {
throw new MojoFailureException("No <parent> found");
}
if (!parent.getDependencyConflictId().equals("org.jenkins-ci.plugins:plugin:pom")) {
throw new MojoFailureException("Unexpected <parent> " + parent);
}
if (new ComparableVersion(parent.getVersion()).compareTo(new ComparableVersion(MINIMUM_PARENT)) < 0) {
PomHelper.setProjectParentVersion(pom, MINIMUM_PARENT);
}
prependProperty(pom, "changelist", "-SNAPSHOT");
prependProperty(pom, "revision", m.group(1));
PomHelper.setProjectValue(pom, "/project/scm/tag", "${scmTag}");
}

private void prependProperty(ModifiedPomXMLEventReader pom, String name, String value) throws XMLStreamException, MojoFailureException {
Stack<String> stack = new Stack<>();
pom.rewind();
boolean found = false;
while (pom.hasNext()) {
XMLEvent event = pom.nextEvent();
if (event.isStartElement()) {
stack.push(event.asStartElement().getName().getLocalPart());
if (stack.equals(Arrays.asList("project", "properties"))) {
pom.mark(0);
}
} else if (event.isEndElement()) {
if (stack.equals(Arrays.asList("project", "properties"))) {
pom.mark(1);
found = true;
String orig = pom.getBetween(0, 1);
// TODO sniff existing indentation
pom.replaceBetween(0, 1, "\n <" + name + ">" + value + "</" + name + ">" + orig);
pom.clearMark(0);
pom.clearMark(1);
}
stack.pop();
}
}
if (!found) {
throw new MojoFailureException("failed to find <properties>");
}
}

}
@@ -0,0 +1,7 @@
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>io.jenkins.tools.incrementals</groupId>
<artifactId>git-changelist-maven-extension</artifactId>
<version>@VERSION@</version>
</extension>
</extensions>

0 comments on commit 6c44568

Please sign in to comment.