|
| 1 | +package com.igormaznitsa.jbbp.mvn.plugin; |
| 2 | + |
| 3 | +import com.igormaznitsa.jbbp.mvn.plugin.converters.Target; |
| 4 | +import com.igormaznitsa.meta.annotation.MustNotContainNull; |
| 5 | +import org.apache.maven.plugin.AbstractMojo; |
| 6 | +import org.apache.maven.plugin.MojoExecutionException; |
| 7 | +import org.apache.maven.plugin.MojoFailureException; |
| 8 | +import org.apache.maven.plugins.annotations.Parameter; |
| 9 | +import org.apache.maven.project.MavenProject; |
| 10 | +import org.codehaus.plexus.compiler.util.scan.InclusionScanException; |
| 11 | +import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner; |
| 12 | +import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner; |
| 13 | +import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping; |
| 14 | + |
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import javax.annotation.Nullable; |
| 17 | +import java.io.File; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +public abstract class AbstractJBBPMojo extends AbstractMojo { |
| 22 | + @Parameter(property = "project", required = true, readonly = true) |
| 23 | + protected MavenProject project; |
| 24 | + |
| 25 | + /** |
| 26 | + * Specifies whether sources are added to the {@code compile} or {@code test} |
| 27 | + * scope. |
| 28 | + */ |
| 29 | + @Parameter(alias = "generateTestSources", defaultValue = "false") |
| 30 | + protected boolean generateTestSources; |
| 31 | + /** |
| 32 | + * Package to override package name extracted from script file name, it will be |
| 33 | + * common for all processed classes. |
| 34 | + */ |
| 35 | + @Parameter(alias = "commonPackage") |
| 36 | + protected String commonPackage; |
| 37 | + /** |
| 38 | + * Target for source generation. |
| 39 | + */ |
| 40 | + @Parameter(alias = "target", defaultValue = "JAVA_1_6") |
| 41 | + protected String target; |
| 42 | + /** |
| 43 | + * Provides an explicit list of all the JBBP scripts that should be included |
| 44 | + * in the generate phase of the plug-in. |
| 45 | + * <p> |
| 46 | + * A set of Ant-like inclusion patterns used to select files from the source |
| 47 | + * directory for processing. By default, the pattern |
| 48 | + * <code>**/*.jbbp</code> is used to select JBBP script files. |
| 49 | + * </p> |
| 50 | + */ |
| 51 | + @Parameter(alias = "includes") |
| 52 | + protected final Set<String> includes = new HashSet<String>(); |
| 53 | + /** |
| 54 | + * A set of Ant-like exclusion patterns used to prevent certain files from |
| 55 | + * being processed. By default, this set is empty such that no files are |
| 56 | + * excluded. |
| 57 | + */ |
| 58 | + @Parameter(alias = "excludes") |
| 59 | + protected final Set<String> excludes = new HashSet<String>(); |
| 60 | + /** |
| 61 | + * Flag to skip processing of the plug-in. |
| 62 | + */ |
| 63 | + @Parameter(alias = "skip", defaultValue = "false") |
| 64 | + protected boolean skip; |
| 65 | + /** |
| 66 | + * Destination directory for generated Java classes, the default value is |
| 67 | + * "${project.build.directory}/generated-sources/jbbp" |
| 68 | + */ |
| 69 | + @Parameter(alias = "outputDirectory", defaultValue = "${project.build.directory}/generated-sources/jbbp") |
| 70 | + protected File outputDirectory; |
| 71 | + /** |
| 72 | + * Flag to make plugin verbose. |
| 73 | + */ |
| 74 | + @Parameter(alias = "verbose", defaultValue = "false") |
| 75 | + protected boolean verbose; |
| 76 | + /** |
| 77 | + * Source directory for JBBP scripts, the default value is |
| 78 | + * "${project.basedir}/src/jbbp" |
| 79 | + */ |
| 80 | + @Parameter(alias = "sourceDirectory", defaultValue = "${project.basedir}/src/jbbp") |
| 81 | + protected File sourceDirectory; |
| 82 | + |
| 83 | + @Nullable |
| 84 | + public String getTarget() { |
| 85 | + return this.target; |
| 86 | + } |
| 87 | + |
| 88 | + public void setTarget(@Nullable final String value) { |
| 89 | + this.target = value; |
| 90 | + } |
| 91 | + |
| 92 | + @MustNotContainNull |
| 93 | + @Nonnull |
| 94 | + public Set<String> getExcludes() { |
| 95 | + return this.excludes; |
| 96 | + } |
| 97 | + |
| 98 | + @Nullable |
| 99 | + public String getCommonPackage() { |
| 100 | + return this.commonPackage; |
| 101 | + } |
| 102 | + |
| 103 | + public void setCommonPackage(@Nullable final String text) { |
| 104 | + this.commonPackage = text; |
| 105 | + } |
| 106 | + |
| 107 | + @MustNotContainNull |
| 108 | + @Nonnull |
| 109 | + public Set<String> getIncludes() { |
| 110 | + return this.includes; |
| 111 | + } |
| 112 | + |
| 113 | + @Nonnull |
| 114 | + public File getSourceDirectory() { |
| 115 | + return this.sourceDirectory; |
| 116 | + } |
| 117 | + |
| 118 | + public void setSourceDirectory(@Nonnull final File file) { |
| 119 | + if (file == null) throw new NullPointerException("File must not be null"); |
| 120 | + this.sourceDirectory = file; |
| 121 | + } |
| 122 | + |
| 123 | + public boolean getSkip() { |
| 124 | + return this.skip; |
| 125 | + } |
| 126 | + |
| 127 | + public void setSkip(final boolean value) { |
| 128 | + this.skip = value; |
| 129 | + } |
| 130 | + |
| 131 | + @Nonnull |
| 132 | + public File getOutputDirectory() { |
| 133 | + return this.outputDirectory; |
| 134 | + } |
| 135 | + |
| 136 | + public boolean getVerbose() { |
| 137 | + return this.verbose; |
| 138 | + } |
| 139 | + |
| 140 | + public void setVerbose(final boolean flag) { |
| 141 | + this.verbose = flag; |
| 142 | + } |
| 143 | + |
| 144 | + public void setGenerateTestSources(final boolean flag) { |
| 145 | + this.generateTestSources = flag; |
| 146 | + } |
| 147 | + |
| 148 | + public boolean getGenerateTestSources() { |
| 149 | + return this.generateTestSources; |
| 150 | + } |
| 151 | + |
| 152 | + protected void registerSourceRoot(@Nonnull final File outputDir) { |
| 153 | + if (this.generateTestSources) { |
| 154 | + getLog().debug("Registering TEST source root : " + outputDir.getPath()); |
| 155 | + this.project.addTestCompileSourceRoot(outputDir.getPath()); |
| 156 | + } else { |
| 157 | + getLog().debug("Registering source root : " + outputDir.getPath()); |
| 158 | + this.project.addCompileSourceRoot(outputDir.getPath()); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Nonnull |
| 163 | + public Set<File> findSources(@Nonnull final File targetDirectory) throws MojoExecutionException { |
| 164 | + try { |
| 165 | + final SourceInclusionScanner scanner = new SimpleSourceInclusionScanner(this.includes, this.excludes); |
| 166 | + scanner.addSourceMapping(new SuffixMapping("JBBP", "jbbp")); |
| 167 | + return scanner.getIncludedSources(this.sourceDirectory, targetDirectory); |
| 168 | + } catch (InclusionScanException ex) { |
| 169 | + throw new MojoExecutionException("Error during sources scanning", ex); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public void logInfo(@Nullable final String text, final boolean onlyIfVerbose) { |
| 174 | + if (text != null) { |
| 175 | + if (this.verbose) { |
| 176 | + getLog().info(text); |
| 177 | + } else { |
| 178 | + if (!onlyIfVerbose) { |
| 179 | + getLog().info(text); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + public boolean checkSetNonEmptyWithLogging(@Nonnull final Set<File> files) { |
| 186 | + boolean result = true; |
| 187 | + if (files.isEmpty()) { |
| 188 | + getLog().warn("There is not any detected JBBP script"); |
| 189 | + result = false; |
| 190 | + } |
| 191 | + return result; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public final void execute() throws MojoExecutionException, MojoFailureException { |
| 196 | + if (this.skip) { |
| 197 | + logInfo("Skip processing", false); |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + if (this.includes.isEmpty()) { |
| 202 | + this.includes.add("**/*.jbbp"); |
| 203 | + } |
| 204 | + |
| 205 | + executeMojo(); |
| 206 | + } |
| 207 | + |
| 208 | + @Nonnull |
| 209 | + public Target findTarget() throws MojoExecutionException { |
| 210 | + final Target theTarget; |
| 211 | + try { |
| 212 | + return Target.valueOf(this.target); |
| 213 | + } catch (IllegalArgumentException ex) { |
| 214 | + throw new MojoExecutionException("Unsupported target : " + this.target, ex); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException; |
| 219 | +} |
0 commit comments