diff --git a/.gitignore b/.gitignore index aeccd84f..aeae089c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ idea-plugin/out .project .classpath .settings +/maven-plugin/src/it/*/interpolated-pom.xml.bak +/maven-plugin/src/it/*/build.log diff --git a/maven-plugin/src/it/mojo-description/expected.log b/maven-plugin/src/it/mojo-description/expected.log new file mode 100644 index 00000000..6e05d235 --- /dev/null +++ b/maven-plugin/src/it/mojo-description/expected.log @@ -0,0 +1,216 @@ +Name: SortPom Plugin +Description: A Maven plugin that sorts the pom.xml file. +Group Id: com.github.ekryd.sortpom +Artifact Id: sortpom-maven-plugin +Version: @pom.version@ +Goal Prefix: sortpom + +This plugin has 3 goals: + +sortpom:help + Description: Display help information on sortpom-maven-plugin. + Call mvn sortpom:help -Ddetail=true -Dgoal= to display parameter + details. + Implementation: sortpom.HelpMojo + Language: java + + Available parameters: + + detail (Default: false) + User property: detail + If true, display all settable properties for each goal. + + goal + User property: goal + The name of the goal for which to show help. If unspecified, all goals + will be displayed. + + indentSize (Default: 2) + User property: indentSize + The number of spaces per indentation level, should be positive. + + lineLength (Default: 80) + User property: lineLength + The maximum length of a display line, should be positive. + +sortpom:sort + Description: Sorts the pom.xml for a Maven project. + Implementation: sortpom.SortMojo + Language: java + Bound to phase: validate + + Available parameters: + + backupFileExtension (Default: .bak) + User property: sort.backupFileExtension + Name of the file extension for the backup file. + + createBackupFile (Default: true) + User property: sort.createBackupFile + Should a backup copy be created for the sorted pom. + + encoding (Default: UTF-8) + User property: sort.encoding + Encoding for the files. + + expandEmptyElements (Default: true) + User property: sort.expandEmptyElements + Should empty xml elements be expanded or not. Example: + or + + ignoreLineSeparators (Default: true) + User property: sort.ignoreLineSeparators + Ignore line separators when comparing current POM with sorted one + + indentBlankLines (Default: false) + User property: sort.indentBlankLines + Should blank lines (if preserved) have indentation. + + keepBlankLines (Default: false) + User property: sort.keepBlankLines + Should blank lines in the pom-file be preserved. A maximum of one line is + preserved between each tag. + + lineSeparator (Default: ${line.separator}) + User property: sort.lineSeparator + Line separator for sorted pom. Can be either \n, \r or \r\n + + nrOfIndentSpace (Default: 2) + User property: sort.nrOfIndentSpace + Number of space characters to use as indentation. A value of -1 indicates + that tab character should be used instead. + + pomFile (Default: ${project.file}) + User property: sort.pomFile + This is the File instance that refers to the location of the pom that + should be sorted. + + predefinedSortOrder + User property: sort.predefinedSortOrder + Choose between a number of predefined sort order files. + + skip (Default: false) + User property: sort.skip + Set this to 'true' to bypass sortpom plugin + + sortDependencies + User property: sort.sortDependencies + Comma-separated ordered list how dependencies should be sorted. Example: + scope,groupId,artifactId. If scope is specified in the list then the + scope ranking is COMPILE, PROVIDED, SYSTEM, RUNTIME, IMPORT and TEST. The + list can be separated by ',;:' + + sortModules (Default: false) + User property: sort.sortModules + Should the Maven pom sub modules be sorted alphabetically. + + sortOrderFile + User property: sort.sortOrderFile + Custom sort order file. + + sortPlugins + User property: sort.sortPlugins + Comma-separated ordered list how plugins should be sorted. Example: + groupId,artifactId The list can be separated by ',;:' + + sortProperties (Default: false) + User property: sort.sortProperties + Should the Maven pom properties be sorted alphabetically. Affects both + project/properties and project/profiles/profile/properties + +sortpom:verify + Description: Verifies that the pom.xml is sorted. If the verification fails + then the pom.xml is sorted. + Implementation: sortpom.VerifyMojo + Language: java + Bound to phase: validate + + Available parameters: + + backupFileExtension (Default: .bak) + User property: sort.backupFileExtension + Name of the file extension for the backup file. + + createBackupFile (Default: true) + User property: sort.createBackupFile + Should a backup copy be created for the sorted pom. + + encoding (Default: UTF-8) + User property: sort.encoding + Encoding for the files. + + expandEmptyElements (Default: true) + User property: sort.expandEmptyElements + Should empty xml elements be expanded or not. Example: + or + + indentBlankLines (Default: false) + User property: sort.indentBlankLines + Should blank lines (if preserved) have indentation. + + keepBlankLines (Default: false) + User property: sort.keepBlankLines + Should blank lines in the pom-file be preserved. A maximum of one line is + preserved between each tag. + + lineSeparator (Default: ${line.separator}) + User property: sort.lineSeparator + Line separator for sorted pom. Can be either \n, \r or \r\n + + nrOfIndentSpace (Default: 2) + User property: sort.nrOfIndentSpace + Number of space characters to use as indentation. A value of -1 indicates + that tab character should be used instead. + + pomFile (Default: ${project.file}) + User property: sort.pomFile + This is the File instance that refers to the location of the pom that + should be sorted. + + predefinedSortOrder + User property: sort.predefinedSortOrder + Choose between a number of predefined sort order files. + + skip (Default: false) + User property: sort.skip + Set this to 'true' to bypass sortpom plugin + + sortDependencies + User property: sort.sortDependencies + Comma-separated ordered list how dependencies should be sorted. Example: + scope,groupId,artifactId. If scope is specified in the list then the + scope ranking is COMPILE, PROVIDED, SYSTEM, RUNTIME, IMPORT and TEST. The + list can be separated by ',;:' + + sortModules (Default: false) + User property: sort.sortModules + Should the Maven pom sub modules be sorted alphabetically. + + sortOrderFile + User property: sort.sortOrderFile + Custom sort order file. + + sortPlugins + User property: sort.sortPlugins + Comma-separated ordered list how plugins should be sorted. Example: + groupId,artifactId The list can be separated by ',;:' + + sortProperties (Default: false) + User property: sort.sortProperties + Should the Maven pom properties be sorted alphabetically. Affects both + project/properties and project/profiles/profile/properties + + verifyFail (Default: sort) + User property: sort.verifyFail + What should happen if verification fails. Can be either 'sort', 'warn' or + 'stop' + + violationFilename + User property: sort.violationFilename + Saves the verification failure to an external xml file, recommended + filename is 'target/sortpom_reports/violation.xml'. + + +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD SUCCESS +[INFO] ------------------------------------------------------------------------ diff --git a/maven-plugin/src/it/mojo-description/invoker.properties b/maven-plugin/src/it/mojo-description/invoker.properties new file mode 100644 index 00000000..6330ba56 --- /dev/null +++ b/maven-plugin/src/it/mojo-description/invoker.properties @@ -0,0 +1 @@ +invoker.goals=help:describe \ No newline at end of file diff --git a/maven-plugin/src/it/mojo-description/pom.xml b/maven-plugin/src/it/mojo-description/pom.xml new file mode 100644 index 00000000..fbc9955d --- /dev/null +++ b/maven-plugin/src/it/mojo-description/pom.xml @@ -0,0 +1,29 @@ + + + SortPom Plugin :: ITs :: Default configuration + 4.0.0 + default-configuration + com.github.ekryd.sortpom.its + 0.0.1-SNAPSHOT + pom + Test default parameters of the plugin + + UTF-8 + + + + + + org.apache.maven.plugins + maven-help-plugin + 2.2 + + com.github.ekryd.sortpom + sortpom-maven-plugin + @pom.version@ + true + + + + + diff --git a/maven-plugin/src/it/mojo-description/postbuild.groovy b/maven-plugin/src/it/mojo-description/postbuild.groovy new file mode 100644 index 00000000..650232f3 --- /dev/null +++ b/maven-plugin/src/it/mojo-description/postbuild.groovy @@ -0,0 +1,7 @@ +log = new File(basedir, 'build.log') +expected_log = new File(basedir, 'expected.log') + +assert log.exists() +assert log.text.contains(expected_log.text.replaceAll('@pom.version@', projectversion)) + +return true \ No newline at end of file diff --git a/maven-plugin/src/main/java/sortpom/AbstractParentMojo.java b/maven-plugin/src/main/java/sortpom/AbstractParentMojo.java new file mode 100644 index 00000000..c2263d2e --- /dev/null +++ b/maven-plugin/src/main/java/sortpom/AbstractParentMojo.java @@ -0,0 +1,139 @@ +package sortpom; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Parameter; + +import java.io.File; + +/** + * Common parent for both SortMojo and VerifyMojo + */ +public abstract class AbstractParentMojo extends AbstractMojo { + + /** + * This is the File instance that refers to the location of the pom that + * should be sorted. + */ + @Parameter(property = "sort.pomFile", defaultValue = "${project.file}") + File pomFile; + + /** + * Should a backup copy be created for the sorted pom. + */ + @Parameter(property = "sort.createBackupFile", defaultValue = "true") + boolean createBackupFile; + + /** + * Name of the file extension for the backup file. + */ + @Parameter(property = "sort.backupFileExtension", defaultValue = ".bak") + String backupFileExtension; + + /** + * Encoding for the files. + */ + @Parameter(property = "sort.encoding", defaultValue = "UTF-8") + String encoding; + + /** + * Line separator for sorted pom. Can be either \n, \r or \r\n + */ + @Parameter(property = "sort.lineSeparator", defaultValue = "${line.separator}") + String lineSeparator; + + /** + * Should empty xml elements be expanded or not. Example: + * <configuration></configuration> or <configuration/> + */ + @Parameter(property = "sort.expandEmptyElements", defaultValue = "true") + boolean expandEmptyElements; + + /** + * Should blank lines in the pom-file be preserved. A maximum of one line is preserved between each tag. + */ + @Parameter(property = "sort.keepBlankLines", defaultValue = "false") + boolean keepBlankLines; + + /** + * Number of space characters to use as indentation. A value of -1 indicates + * that tab character should be used instead. + */ + @Parameter(property = "sort.nrOfIndentSpace", defaultValue = "2") + int nrOfIndentSpace; + + /** + * Should blank lines (if preserved) have indentation. + */ + @Parameter(property = "sort.indentBlankLines", defaultValue = "false") + boolean indentBlankLines; + + /** + * Choose between a number of predefined sort order files. + */ + @Parameter(property = "sort.predefinedSortOrder") + String predefinedSortOrder; + + /** + * Custom sort order file. + */ + @Parameter(property = "sort.sortOrderFile") + String sortOrderFile; + + /** + * Comma-separated ordered list how dependencies should be sorted. Example: scope,groupId,artifactId. + * If scope is specified in the list then the scope ranking is COMPILE, PROVIDED, SYSTEM, RUNTIME, IMPORT and TEST. + * The list can be separated by ",;:" + */ + @Parameter(property = "sort.sortDependencies") + String sortDependencies; + + /** + * Comma-separated ordered list how plugins should be sorted. Example: groupId,artifactId + * The list can be separated by ",;:" + */ + @Parameter(property = "sort.sortPlugins") + String sortPlugins; + + /** + * Should the Maven pom properties be sorted alphabetically. Affects both + * project/properties and project/profiles/profile/properties + */ + @Parameter(property = "sort.sortProperties", defaultValue = "false") + boolean sortProperties; + + /** + * Should the Maven pom sub modules be sorted alphabetically. + */ + @Parameter(property = "sort.sortModules", defaultValue = "false") + boolean sortModules; + + /** + * Set this to 'true' to bypass sortpom plugin + */ + @Parameter(property = "sort.skip", defaultValue = "false") + private boolean skip; + + final SortPomImpl sortPomImpl = new SortPomImpl(); + + /** + * Execute plugin. + * + * @throws org.apache.maven.plugin.MojoFailureException exception that will be handled by plugin framework + * @see org.apache.maven.plugin.Mojo#execute() + */ + @Override + public void execute() throws MojoFailureException { + if (skip) { + getLog().info("Skipping Sortpom"); + } else { + setup(); + sortPom(); + } + + } + + protected abstract void sortPom() throws MojoFailureException; + + protected abstract void setup() throws MojoFailureException; +} diff --git a/maven-plugin/src/main/java/sortpom/SortMojo.java b/maven-plugin/src/main/java/sortpom/SortMojo.java index 58c68f47..22b7316e 100644 --- a/maven-plugin/src/main/java/sortpom/SortMojo.java +++ b/maven-plugin/src/main/java/sortpom/SortMojo.java @@ -1,6 +1,5 @@ package sortpom; -import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -9,8 +8,6 @@ import sortpom.logger.MavenLogger; import sortpom.parameter.PluginParameters; -import java.io.File; - /** * Sorts the pom.xml for a Maven project. * @@ -18,37 +15,7 @@ */ @Mojo(name = "sort", threadSafe = true, defaultPhase = LifecyclePhase.VALIDATE) @SuppressWarnings({"UnusedDeclaration", "JavaDoc"}) -public class SortMojo extends AbstractMojo { - /** - * This is the File instance that refers to the location of the pom that - * should be sorted. - */ - @Parameter(property = "sort.pomFile", defaultValue = "${project.file}") - private File pomFile; - - /** - * Should a backup copy be created for the sorted pom. - */ - @Parameter(property = "sort.createBackupFile", defaultValue = "true") - private boolean createBackupFile; - - /** - * Name of the file extension for the backup file. - */ - @Parameter(property = "sort.backupFileExtension", defaultValue = ".bak") - private String backupFileExtension; - - /** - * Encoding for the files. - */ - @Parameter(property = "sort.encoding", defaultValue = "UTF-8") - private String encoding; - - /** - * Line separator for sorted pom. Can be either \n, \r or \r\n - */ - @Parameter(property = "sort.lineSeparator", defaultValue = "${line.separator}") - private String lineSeparator; +public class SortMojo extends AbstractParentMojo { /** * Ignore line separators when comparing current POM with sorted one @@ -56,97 +23,6 @@ public class SortMojo extends AbstractMojo { @Parameter(property = "sort.ignoreLineSeparators", defaultValue = "true") private boolean ignoreLineSeparators; - /** - * Should empty xml elements be expanded or not. Example: - * <configuration></configuration> or <configuration/> - */ - @Parameter(property = "sort.expandEmptyElements", defaultValue = "true") - private boolean expandEmptyElements; - - /** - * Should blank lines in the pom-file be preserved. A maximum of one line is preserved between each tag. - */ - @Parameter(property = "sort.keepBlankLines", defaultValue = "false") - private boolean keepBlankLines; - - /** - * Number of space characters to use as indentation. A value of -1 indicates - * that tab character should be used instead. - */ - @Parameter(property = "sort.nrOfIndentSpace", defaultValue = "2") - private int nrOfIndentSpace; - - /** - * Should blank lines (if preserved) have indentation. - */ - @Parameter(property = "sort.indentBlankLines", defaultValue = "false") - private boolean indentBlankLines; - - /** - * Choose between a number of predefined sort order files. - */ - @Parameter(property = "sort.predefinedSortOrder") - private String predefinedSortOrder; - - /** - * Custom sort order file. - */ - @Parameter(property = "sort.sortOrderFile") - private String sortOrderFile; - - /** - * Comma-separated ordered list how dependencies should be sorted. Example: scope,groupId,artifactId. - * If scope is specified in the list then the scope ranking is COMPILE, PROVIDED, SYSTEM, RUNTIME, IMPORT and TEST. - * The list can be separated by ",;:" - */ - @Parameter(property = "sort.sortDependencies") - private String sortDependencies; - - /** - * Comma-separated ordered list how plugins should be sorted. Example: groupId,artifactId - * The list can be separated by ",;:" - */ - @Parameter(property = "sort.sortPlugins") - private String sortPlugins; - - /** - * Should the Maven pom properties be sorted alphabetically. Affects both - * project/properties and project/profiles/profile/properties - */ - @Parameter(property = "sort.sortProperties", defaultValue = "false") - private boolean sortProperties; - - /** - * Should the Maven pom sub modules be sorted alphabetically. - */ - @Parameter(property = "sort.sortModules", defaultValue = "false") - private boolean sortModules; - - /** - * Set this to 'true' to bypass sortpom plugin - */ - @Parameter(property = "sort.skip", defaultValue = "false") - private boolean skip; - - private final SortPomImpl sortPomImpl = new SortPomImpl(); - - /** - * Execute plugin. - * - * @throws org.apache.maven.plugin.MojoFailureException exception that will be handled by plugin framework - * @see org.apache.maven.plugin.Mojo#execute() - */ - @Override - public void execute() throws MojoFailureException { - if (skip) { - getLog().info("Skipping sortpom"); - } else { - setup(); - sortPom(); - } - - } - public void setup() throws MojoFailureException { new ExceptionConverter(() -> { PluginParameters pluginParameters = PluginParameters.builder() @@ -164,7 +40,7 @@ public void setup() throws MojoFailureException { }).executeAndConvertException(); } - private void sortPom() throws MojoFailureException { + protected void sortPom() throws MojoFailureException { new ExceptionConverter(sortPomImpl::sortPom).executeAndConvertException(); } diff --git a/maven-plugin/src/main/java/sortpom/VerifyMojo.java b/maven-plugin/src/main/java/sortpom/VerifyMojo.java index 35091d95..393c1287 100644 --- a/maven-plugin/src/main/java/sortpom/VerifyMojo.java +++ b/maven-plugin/src/main/java/sortpom/VerifyMojo.java @@ -1,6 +1,5 @@ package sortpom; -import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -9,8 +8,6 @@ import sortpom.logger.MavenLogger; import sortpom.parameter.PluginParameters; -import java.io.File; - /** * Verifies that the pom.xml is sorted. If the verification fails then the pom.xml is sorted. * @@ -18,141 +15,20 @@ */ @Mojo(name = "verify", threadSafe = true, defaultPhase = LifecyclePhase.VALIDATE) @SuppressWarnings({"UnusedDeclaration"}) -public class VerifyMojo extends AbstractMojo { - /** - * This is the File instance that refers to the location of the pom that - * should be sorted. - */ - @Parameter(property = "sort.pomFile", defaultValue = "${project.file}") - private File pomFile; - - /** - * Choose between a number of predefined sort order files. - */ - @Parameter(property = "sort.predefinedSortOrder") - private String predefinedSortOrder; - - /** - * Custom sort order file. - */ - @Parameter(property = "sort.sortOrderFile") - private String sortOrderFile; - - /** - * Comma-separated ordered list how dependencies should be sorted. Example: scope,groupId,artifactId - * If scope is specified in the list then the scope ranking is COMPILE, PROVIDED, SYSTEM, RUNTIME, IMPORT and TEST. - * The list can be separated by ",;:" - */ - @Parameter(property = "sort.sortDependencies") - private String sortDependencies; - - /** - * Comma-separated ordered list how plugins should be sorted. Example: groupId,artifactId - * The list can be separated by ",;:" - */ - @Parameter(property = "sort.sortPlugins") - private String sortPlugins; - - /** - * Should the Maven pom properties be sorted alphabetically. Affects both - * project/properties and project/profiles/profile/properties - */ - @Parameter(property = "sort.sortProperties", defaultValue = "false") - private boolean sortProperties; - - /** - * Should the Maven pom sub modules be sorted alphabetically. - */ - @Parameter(property = "sort.sortModules", defaultValue = "false") - private boolean sortModules; - - /** - * Encoding for the files. - */ - @Parameter(property = "sort.encoding", defaultValue = "UTF-8") - private String encoding; +public class VerifyMojo extends AbstractParentMojo { /** * What should happen if verification fails. Can be either 'sort', 'warn' or 'stop' */ @Parameter(property = "sort.verifyFail", defaultValue = "sort") private String verifyFail; - - - /** - * Should a backup copy be created for the sorted pom. - */ - @Parameter(property = "sort.createBackupFile", defaultValue = "true") - private boolean createBackupFile; - - /** - * Name of the file extension for the backup file. - */ - @Parameter(property = "sort.backupFileExtension", defaultValue = ".bak") - private String backupFileExtension; - + /** * Saves the verification failure to an external xml file, recommended filename is 'target/sortpom_reports/violation.xml'. */ @Parameter(property = "sort.violationFilename") private String violationFilename; - - /** - * Line separator for sorted pom. Can be either \n, \r or \r\n - */ - @Parameter(property = "sort.lineSeparator", defaultValue = "${line.separator}") - private String lineSeparator; - - /** - * Should empty xml elements be expanded or not. Example: - * <configuration></configuration> or <configuration/> - */ - @Parameter(property = "sort.expandEmptyElements", defaultValue = "true") - private boolean expandEmptyElements; - - /** - * Should blank lines in the pom-file be preserved. A maximum of one line is preserved between each tag. - */ - @Parameter(property = "sort.keepBlankLines", defaultValue = "false") - private boolean keepBlankLines; - - /** - * Number of space characters to use as indentation. A value of -1 indicates - * that tab character should be used instead. - */ - @Parameter(property = "sort.nrOfIndentSpace", defaultValue = "2") - private int nrOfIndentSpace; - - /** - * Should blank lines (if preserved) have indentation. - */ - @Parameter(property = "sort.indentBlankLines", defaultValue = "false") - private boolean indentBlankLines; - - /** - * Set this to 'true' to bypass sortpom plugin - */ - @Parameter(property = "sort.skip", defaultValue = "false") - private boolean skip; - - private final SortPomImpl sortPomImpl = new SortPomImpl(); - - /** - * Execute plugin. - * - * @throws org.apache.maven.plugin.MojoFailureException exception that will be handled by plugin framework - * @see org.apache.maven.plugin.Mojo#execute() - */ - @Override - public void execute() throws MojoFailureException { - if (skip) { - getLog().info("Skipping Sortpom"); - } else { - setup(); - sortPom(); - } - } - + public void setup() throws MojoFailureException { new ExceptionConverter(() -> { @@ -172,7 +48,7 @@ public void setup() throws MojoFailureException { }).executeAndConvertException(); } - private void sortPom() throws MojoFailureException { + protected void sortPom() throws MojoFailureException { new ExceptionConverter(sortPomImpl::verifyPom).executeAndConvertException(); }