|
| 1 | +package io.swagger.v3.oas.transformer; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.apache.maven.artifact.Artifact; |
| 10 | +import org.apache.maven.plugin.AbstractMojo; |
| 11 | +import org.apache.maven.plugin.MojoFailureException; |
| 12 | +import org.apache.maven.plugins.annotations.Component; |
| 13 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 14 | +import org.apache.maven.plugins.annotations.Mojo; |
| 15 | +import org.apache.maven.plugins.annotations.Parameter; |
| 16 | +import org.apache.maven.plugins.annotations.ResolutionScope; |
| 17 | +import org.apache.maven.project.MavenProject; |
| 18 | +import org.apache.maven.project.MavenProjectHelper; |
| 19 | +import org.eclipse.transformer.Transformer; |
| 20 | +import org.eclipse.transformer.jakarta.JakartaTransformer; |
| 21 | + |
| 22 | +/** |
| 23 | + * |
| 24 | + * Adapted from https://github.com/eclipse/transformer/blob/main/org.eclipse.transformer.maven |
| 25 | + * not to add the transformed JAR as an attached artifact to the project. |
| 26 | + * |
| 27 | + * To be replaced with Eclipse original Transformer plugin when/if optional attachment is supported. |
| 28 | + * |
| 29 | + * This is a Maven plugin which runs the Eclipse Transformer on build artifacts |
| 30 | + * as part of the build. |
| 31 | + */ |
| 32 | +@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM, defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true) |
| 33 | +public class TransformMojo extends AbstractMojo { |
| 34 | + |
| 35 | + @Parameter(defaultValue = "${project}", readonly = true, required = true) |
| 36 | + private MavenProject project; |
| 37 | + |
| 38 | + @Parameter(defaultValue = "false", property = "transformer-plugin.invert", required = true) |
| 39 | + private Boolean invert; |
| 40 | + |
| 41 | + @Parameter(defaultValue = "true", property = "transformer-plugin.overwrite", required = true) |
| 42 | + private Boolean overwrite; |
| 43 | + |
| 44 | + @Parameter(property = "transformer-plugin.renames", defaultValue = "") |
| 45 | + private String rulesRenamesUri; |
| 46 | + |
| 47 | + @Parameter(property = "transformer-plugin.versions", defaultValue = "") |
| 48 | + private String rulesVersionUri; |
| 49 | + |
| 50 | + @Parameter(property = "transformer-plugin.bundles", defaultValue = "") |
| 51 | + private String rulesBundlesUri; |
| 52 | + |
| 53 | + @Parameter(property = "transformer-plugin.direct", defaultValue = "") |
| 54 | + private String rulesDirectUri; |
| 55 | + |
| 56 | + @Parameter(property = "transformer-plugin.per-class-constant", defaultValue = "") |
| 57 | + private String rulesPerClassConstantUri; |
| 58 | + |
| 59 | + @Parameter(property = "transformer-plugin.xml", defaultValue = "") |
| 60 | + private String rulesXmlsUri; |
| 61 | + |
| 62 | + @Parameter(defaultValue = "transformed") |
| 63 | + private String classifier; |
| 64 | + |
| 65 | + @Parameter(defaultValue = "${project.build.directory}", required = true) |
| 66 | + private File outputDirectory; |
| 67 | + |
| 68 | + @Parameter(defaultValue = "true", property = "transformer-plugin.attach", required = true) |
| 69 | + private Boolean attach; |
| 70 | + |
| 71 | + @Component |
| 72 | + private MavenProjectHelper projectHelper; |
| 73 | + |
| 74 | + /** |
| 75 | + * Main execution point of the plugin. This looks at the attached artifacts, |
| 76 | + * and runs the transformer on them. |
| 77 | + * |
| 78 | + * @throws MojoFailureException Thrown if there is an error during plugin |
| 79 | + * execution |
| 80 | + */ |
| 81 | + @Override |
| 82 | + public void execute() throws MojoFailureException { |
| 83 | + final Transformer transformer = getTransformer(); |
| 84 | + |
| 85 | + final Artifact[] sourceArtifacts = getSourceArtifacts(); |
| 86 | + for (final Artifact sourceArtifact : sourceArtifacts) { |
| 87 | + transform(transformer, sourceArtifact); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * This runs the transformation process on the source artifact with the |
| 93 | + * transformer provided. The transformed artifact is attached to the |
| 94 | + * project. |
| 95 | + * |
| 96 | + * @param transformer The Transformer to use for the transformation |
| 97 | + * @param sourceArtifact The Artifact to transform |
| 98 | + * @throws MojoFailureException if plugin execution fails |
| 99 | + */ |
| 100 | + public void transform(final Transformer transformer, final Artifact sourceArtifact) throws MojoFailureException { |
| 101 | + |
| 102 | + final String sourceClassifier = sourceArtifact.getClassifier(); |
| 103 | + final String targetClassifier = (sourceClassifier == null || sourceClassifier.length() == 0) ? this.classifier |
| 104 | + : sourceClassifier + "-" + this.classifier; |
| 105 | + |
| 106 | + final File targetFile = new File(outputDirectory, sourceArtifact.getArtifactId() + "-" + targetClassifier + "-" |
| 107 | + + sourceArtifact.getVersion() + "." + sourceArtifact.getType()); |
| 108 | + |
| 109 | + final List<String> args = new ArrayList<>(); |
| 110 | + args.add(sourceArtifact.getFile() |
| 111 | + .getAbsolutePath()); |
| 112 | + args.add(targetFile.getAbsolutePath()); |
| 113 | + |
| 114 | + if (this.overwrite) { |
| 115 | + args.add("-o"); |
| 116 | + } |
| 117 | + |
| 118 | + transformer.setArgs(args.toArray(new String[0])); |
| 119 | + int rc = transformer.run(); |
| 120 | + |
| 121 | + if (rc != 0) { |
| 122 | + throw new MojoFailureException("Transformer failed with an error: " + Transformer.RC_DESCRIPTIONS[rc]); |
| 123 | + } |
| 124 | + |
| 125 | + if (attach) { |
| 126 | + projectHelper.attachArtifact(project, sourceArtifact.getType(), targetClassifier, targetFile); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Builds a configured transformer for the specified source and target |
| 132 | + * artifacts |
| 133 | + * |
| 134 | + * @return A configured transformer |
| 135 | + */ |
| 136 | + public Transformer getTransformer() { |
| 137 | + final Transformer transformer = new Transformer(System.out, System.err); |
| 138 | + transformer.setOptionDefaults(JakartaTransformer.class, getOptionDefaults()); |
| 139 | + return transformer; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Gets the source artifacts that should be transformed |
| 144 | + * |
| 145 | + * @return an array to artifacts to be transformed |
| 146 | + */ |
| 147 | + public Artifact[] getSourceArtifacts() { |
| 148 | + List<Artifact> artifactList = new ArrayList<>(); |
| 149 | + if (project.getArtifact() != null && project.getArtifact() |
| 150 | + .getFile() != null) { |
| 151 | + artifactList.add(project.getArtifact()); |
| 152 | + } |
| 153 | + |
| 154 | + for (final Artifact attachedArtifact : project.getAttachedArtifacts()) { |
| 155 | + if (attachedArtifact.getFile() != null) { |
| 156 | + artifactList.add(attachedArtifact); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return artifactList.toArray(new Artifact[0]); |
| 161 | + } |
| 162 | + |
| 163 | + private Map<Transformer.AppOption, String> getOptionDefaults() { |
| 164 | + Map<Transformer.AppOption, String> optionDefaults = new HashMap<>(); |
| 165 | + optionDefaults.put(Transformer.AppOption.RULES_RENAMES, |
| 166 | + isEmpty(rulesRenamesUri) ? "jakarta-renames.properties" : rulesRenamesUri); |
| 167 | + optionDefaults.put(Transformer.AppOption.RULES_VERSIONS, |
| 168 | + isEmpty(rulesVersionUri) ? "jakarta-versions.properties" : rulesVersionUri); |
| 169 | + optionDefaults.put(Transformer.AppOption.RULES_BUNDLES, |
| 170 | + isEmpty(rulesBundlesUri) ? "jakarta-bundles.properties" : rulesBundlesUri); |
| 171 | + optionDefaults.put(Transformer.AppOption.RULES_DIRECT, |
| 172 | + isEmpty(rulesDirectUri) ? "jakarta-direct.properties" : rulesDirectUri); |
| 173 | + optionDefaults.put(Transformer.AppOption.RULES_MASTER_TEXT, |
| 174 | + isEmpty(rulesXmlsUri) ? "jakarta-text-master.properties" : rulesXmlsUri); |
| 175 | + optionDefaults.put(Transformer.AppOption.RULES_PER_CLASS_CONSTANT, |
| 176 | + isEmpty(rulesPerClassConstantUri) ? "jakarta-per-class-constant-master.properties" : rulesPerClassConstantUri); |
| 177 | + return optionDefaults; |
| 178 | + } |
| 179 | + |
| 180 | + private boolean isEmpty(final String input) { |
| 181 | + return input == null || input.trim() |
| 182 | + .length() == 0; |
| 183 | + } |
| 184 | + |
| 185 | + void setProject(MavenProject project) { |
| 186 | + this.project = project; |
| 187 | + } |
| 188 | + |
| 189 | + void setClassifier(String classifier) { |
| 190 | + this.classifier = classifier; |
| 191 | + } |
| 192 | + |
| 193 | + MavenProjectHelper getProjectHelper() { |
| 194 | + return projectHelper; |
| 195 | + } |
| 196 | + |
| 197 | + void setProjectHelper(MavenProjectHelper projectHelper) { |
| 198 | + this.projectHelper = projectHelper; |
| 199 | + } |
| 200 | + |
| 201 | + void setOverwrite(Boolean overwrite) { |
| 202 | + this.overwrite = overwrite; |
| 203 | + } |
| 204 | + |
| 205 | + void setOutputDirectory(File outputDirectory) { |
| 206 | + this.outputDirectory = outputDirectory; |
| 207 | + } |
| 208 | + |
| 209 | + void setAttach(Boolean attach) { |
| 210 | + this.attach = attach; |
| 211 | + } |
| 212 | +} |
0 commit comments