-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from jgallimore/mojo
Add simple Maven Plugin
- Loading branch information
Showing
6 changed files
with
656 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: (EPL-2.0 OR Apache-2.0) | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.eclipse.transformer</groupId> | ||
<artifactId>org.eclipse.transformer.parent</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>org.eclipse.transformer.maven</artifactId> | ||
<description>Eclipse Transformer :: Maven Plugin</description> | ||
<packaging>maven-plugin</packaging> | ||
<name>${project.groupId}:${project.artifactId}</name> | ||
<url>https://projects.eclipse.org/projects/technology.transformer</url> | ||
|
||
|
||
<properties> | ||
<maven-plugin.prefix>transform</maven-plugin.prefix> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.transformer</groupId> | ||
<artifactId>org.eclipse.transformer.cli</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
<!-- needed in compile scope to instantiate Provider org.eclipse.sisu.space.SisuIndexAPT6 --> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-plugin-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-tools</groupId> | ||
<artifactId>maven-plugin-annotations</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.plexus</groupId> | ||
<artifactId>plexus-utils</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-plugin-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-model</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-cli</groupId> | ||
<artifactId>commons-cli</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-artifact</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-compat</artifactId> | ||
</dependency> | ||
|
||
<!-- TESTS --> | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-testing</groupId> | ||
<artifactId>maven-plugin-testing-harness</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-aether-provider</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-migrationsupport</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.shrinkwrap</groupId> | ||
<artifactId>shrinkwrap-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.shrinkwrap</groupId> | ||
<artifactId>shrinkwrap-impl-base</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax</groupId> | ||
<artifactId>javaee-api</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive combine.self="override"/> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
194 changes: 194 additions & 0 deletions
194
org.eclipse.transformer.maven/src/main/java/org/eclipse/transformer/maven/TransformMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: (EPL-2.0 OR Apache-2.0) | ||
********************************************************************************/ | ||
|
||
package org.eclipse.transformer.maven; | ||
|
||
import org.apache.maven.artifact.Artifact; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
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.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.plugins.annotations.ResolutionScope; | ||
import org.apache.maven.project.MavenProject; | ||
import org.apache.maven.project.MavenProjectHelper; | ||
import org.eclipse.transformer.Transformer; | ||
import org.eclipse.transformer.jakarta.JakartaTransformer; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* This is a Maven plugin which runs the Eclipse Transformer on build artifacts as part of the build. | ||
*/ | ||
@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM, defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true) | ||
public class TransformMojo extends AbstractMojo { | ||
|
||
@Parameter(defaultValue = "${project}", readonly = true, required = true) | ||
private MavenProject project; | ||
|
||
@Parameter(defaultValue = "false", property = "transformer-plugin.invert", required = true) | ||
private Boolean invert; | ||
|
||
@Parameter(defaultValue = "true", property = "transformer-plugin.overwrite", required = true) | ||
private Boolean overwrite; | ||
|
||
@Parameter(property = "transformer-plugin.renames", defaultValue = "") | ||
private String rulesRenamesUri; | ||
|
||
@Parameter(property = "transformer-plugin.versions", defaultValue = "") | ||
private String rulesVersionUri; | ||
|
||
@Parameter(property = "transformer-plugin.bundles", defaultValue = "") | ||
private String rulesBundlesUri; | ||
|
||
@Parameter(property = "transformer-plugin.direct", defaultValue = "") | ||
private String rulesDirectUri; | ||
|
||
@Parameter(property = "transformer-plugin.xml", defaultValue = "") | ||
private String rulesXmlsUri; | ||
|
||
@Parameter(defaultValue = "transformed") | ||
private String classifier; | ||
|
||
@Parameter(defaultValue = "${project.build.directory}", required = true) | ||
private File outputDirectory; | ||
|
||
@Component | ||
private MavenProjectHelper projectHelper; | ||
|
||
/** | ||
* Main execution point of the plugin. This looks at the attached artifacts, and runs the transformer on them. | ||
* @throws MojoFailureException Thrown if there is an error during plugin execution | ||
*/ | ||
public void execute() throws MojoFailureException { | ||
final Transformer transformer = getTransformer(); | ||
|
||
final Artifact[] sourceArtifacts = getSourceArtifacts(); | ||
for (final Artifact sourceArtifact : sourceArtifacts) { | ||
transform(transformer, sourceArtifact); | ||
} | ||
} | ||
|
||
/** | ||
* This runs the transformation process on the source artifact with the transformer provided. | ||
* The transformed artifact is attached to the project. | ||
* @param transformer The Transformer to use for the transformation | ||
* @param sourceArtifact The Artifact to transform | ||
* @throws MojoFailureException if plugin execution fails | ||
*/ | ||
public void transform(final Transformer transformer, final Artifact sourceArtifact) throws MojoFailureException { | ||
|
||
final String sourceClassifier = sourceArtifact.getClassifier(); | ||
final String targetClassifier = (sourceClassifier == null || sourceClassifier.length() == 0) ? | ||
this.classifier : sourceClassifier + "-" + this.classifier; | ||
|
||
final File targetFile = new File(outputDirectory, | ||
sourceArtifact.getArtifactId() + "-" + | ||
targetClassifier + "-" + sourceArtifact.getVersion() + "." | ||
+ sourceArtifact.getType()); | ||
|
||
final List<String> args = new ArrayList<>(); | ||
args.add(sourceArtifact.getFile().getAbsolutePath()); | ||
args.add(targetFile.getAbsolutePath()); | ||
|
||
if (this.overwrite) { | ||
args.add("-o"); | ||
} | ||
|
||
transformer.setArgs(args.toArray(new String[0])); | ||
int rc = transformer.run(); | ||
|
||
if (rc != 0) { | ||
throw new MojoFailureException("Transformer failed with an error: " + Transformer.RC_DESCRIPTIONS[rc]); | ||
} | ||
|
||
projectHelper.attachArtifact( | ||
project, | ||
sourceArtifact.getType(), | ||
targetClassifier, | ||
targetFile | ||
); | ||
} | ||
|
||
/** | ||
* Builds a configured transformer for the specified source and target artifacts | ||
* @return A configured transformer | ||
*/ | ||
public Transformer getTransformer() { | ||
final Transformer transformer = new Transformer(System.out, System.err); | ||
transformer.setOptionDefaults(JakartaTransformer.class, getOptionDefaults()); | ||
return transformer; | ||
} | ||
|
||
/** | ||
* Gets the source artifacts that should be transformed | ||
* @return an array to artifacts to be transformed | ||
*/ | ||
public Artifact[] getSourceArtifacts() { | ||
List<Artifact> artifactList = new ArrayList<Artifact>(); | ||
if (project.getArtifact() != null && project.getArtifact().getFile() != null) { | ||
artifactList.add(project.getArtifact()); | ||
} | ||
|
||
for (final Artifact attachedArtifact : project.getAttachedArtifacts()) { | ||
if (attachedArtifact.getFile() != null) { | ||
artifactList.add(attachedArtifact); | ||
} | ||
} | ||
|
||
return artifactList.toArray(new Artifact[0]); | ||
} | ||
|
||
private Map<Transformer.AppOption, String> getOptionDefaults() { | ||
HashMap<Transformer.AppOption, String> optionDefaults = new HashMap(); | ||
optionDefaults.put(Transformer.AppOption.RULES_RENAMES, isEmpty(rulesRenamesUri) ? "jakarta-renames.properties" : rulesRenamesUri); | ||
optionDefaults.put(Transformer.AppOption.RULES_VERSIONS, isEmpty(rulesVersionUri) ? "jakarta-versions.properties" : rulesVersionUri); | ||
optionDefaults.put(Transformer.AppOption.RULES_BUNDLES, isEmpty(rulesBundlesUri) ? "jakarta-bundles.properties" : rulesBundlesUri); | ||
optionDefaults.put(Transformer.AppOption.RULES_DIRECT, isEmpty(rulesDirectUri) ? "jakarta-direct.properties" : rulesDirectUri); | ||
optionDefaults.put(Transformer.AppOption.RULES_MASTER_TEXT, isEmpty(rulesXmlsUri) ? "jakarta-text-master.properties" : rulesXmlsUri); | ||
return optionDefaults; | ||
} | ||
|
||
private boolean isEmpty(final String input) { | ||
return input == null || input.trim().length() == 0; | ||
} | ||
|
||
void setProject(MavenProject project) { | ||
this.project = project; | ||
} | ||
|
||
void setClassifier(String classifier) { | ||
this.classifier = classifier; | ||
} | ||
|
||
MavenProjectHelper getProjectHelper() { | ||
return projectHelper; | ||
} | ||
|
||
void setProjectHelper(MavenProjectHelper projectHelper) { | ||
this.projectHelper = projectHelper; | ||
} | ||
|
||
void setOverwrite(Boolean overwrite) { | ||
this.overwrite = overwrite; | ||
} | ||
|
||
void setOutputDirectory(File outputDirectory) { | ||
this.outputDirectory = outputDirectory; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
org.eclipse.transformer.maven/src/test/java/org/eclipse/transformer/maven/EchoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: (EPL-2.0 OR Apache-2.0) | ||
********************************************************************************/ | ||
|
||
package org.eclipse.transformer.maven; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
|
||
@Path("echo") | ||
public class EchoService { | ||
|
||
@POST | ||
@Produces("text/plain") | ||
@Consumes("text/plain") | ||
public String echo(final String input) { | ||
return input; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.