Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transformer maven plugin fork not to attach transformed JAR #3889

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions modules/swagger-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-eclipse-transformer-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<attach>false</attach>
<classifier>jakarta</classifier>
<outputDirectory>${project.basedir}/../swagger-project-jakarta/modules/${project.artifactId}-jakarta/transformed</outputDirectory>
</configuration>
<executions>
<execution>
<id>transform-jakarta</id>
<goals>
<goal>run</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
19 changes: 19 additions & 0 deletions modules/swagger-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-eclipse-transformer-maven-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<attach>false</attach>
<classifier>jakarta</classifier>
<outputDirectory>${project.basedir}/../swagger-project-jakarta/modules/${project.artifactId}-jakarta/transformed</outputDirectory>
</configuration>
<executions>
<execution>
<id>transform-jakarta</id>
<goals>
<goal>run</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down
6 changes: 6 additions & 0 deletions modules/swagger-eclipse-transformer-maven-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Adapted from https://github.com/eclipse/transformer/blob/main/org.eclipse.transformer.maven
not to add the transformed JAR as an attached artifact to the project.

To be replaced with Eclipse original Transformer plugin when/if optional attachment is supported.


104 changes: 104 additions & 0 deletions modules/swagger-eclipse-transformer-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-project</artifactId>
<version>2.1.7</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>swagger-eclipse-transformer-maven-plugin</artifactId>
<name>swagger-eclipse-transformer-maven-plugin</name>
<description>swagger-eclipse-transformer-maven-plugin</description>
<packaging>maven-plugin</packaging>
<properties>
<maven.target.version>3.2.2</maven.target.version>
<eclipse.transformer.version>0.20</eclipse.transformer.version>
<maven-test-harness.version>3.3.0</maven-test-harness.version>
<shrinkwrap.version>1.2.6</shrinkwrap.version>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
<junit-platform.version>1.7.0</junit-platform.version>
<assertj.version>3.18.1</assertj.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${enforcer-plugin-version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<goalPrefix>transform</goalPrefix>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.transformer</groupId>
<artifactId>org.eclipse.transformer.cli</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<!-- needed in compile scope to instantiate Provider org.eclipse.sisu.space.SisuIndexAPT6 -->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.target.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${maven.target.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.target.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.target.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${maven.target.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package io.swagger.v3.oas.transformer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
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;

/**
*
* Adapted from https://github.com/eclipse/transformer/blob/main/org.eclipse.transformer.maven
* not to add the transformed JAR as an attached artifact to the project.
*
* To be replaced with Eclipse original Transformer plugin when/if optional attachment is supported.
*
* 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.per-class-constant", defaultValue = "")
private String rulesPerClassConstantUri;

@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;

@Parameter(defaultValue = "true", property = "transformer-plugin.attach", required = true)
private Boolean attach;

@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
*/
@Override
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]);
}

if (attach) {
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<>();
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() {
Map<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);
optionDefaults.put(Transformer.AppOption.RULES_PER_CLASS_CONSTANT,
isEmpty(rulesPerClassConstantUri) ? "jakarta-per-class-constant-master.properties" : rulesPerClassConstantUri);
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;
}

void setAttach(Boolean attach) {
this.attach = attach;
}
}
Loading