From 7e3b285d313de1d9dfdfd325139f5b13bf825729 Mon Sep 17 00:00:00 2001 From: VLCEK Martin Date: Fri, 19 Nov 2021 14:46:26 +0100 Subject: [PATCH 1/4] https://github.com/mathieucarbou/license-maven-plugin/issues/242: replace the test for further header lines with an optional regex. --- .../maven/plugin/license/HeaderStyle.java | 8 ++- .../license/header/HeaderDefinition.java | 54 ++++++++++++++++++- .../plugin/license/header/HeaderParser.java | 2 +- .../license/header/HeaderParserTest.java | 29 ++++++++++ .../src/test/resources/doc/java1.txt | 4 ++ .../src/test/resources/doc/java2.txt | 4 ++ .../src/test/resources/doc/java3.txt | 4 ++ 7 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 license-maven-plugin/src/test/resources/doc/java1.txt create mode 100644 license-maven-plugin/src/test/resources/doc/java2.txt create mode 100644 license-maven-plugin/src/test/resources/doc/java3.txt diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java index 8be5d7891..6b0b1e274 100644 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java @@ -94,6 +94,12 @@ public class HeaderStyle { @Parameter(required = true) public String firstLineDetectionPattern; + /** + * The regex used to detect the other lines of a header section or line + */ + @Parameter(required = false) + public String otherLineDetectionPattern; + /** * The regex used to detect the end of a header section or line */ @@ -101,6 +107,6 @@ public class HeaderStyle { public String lastLineDetectionPattern; public HeaderDefinition toHeaderDefinition() { - return new HeaderDefinition(name, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, lastLineDetectionPattern, allowBlankLines, multiline, padLines); + return new HeaderDefinition(name != null ? name.toLowerCase() : null, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, otherLineDetectionPattern, lastLineDetectionPattern, allowBlankLines, multiline, padLines); } } diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java index 8a8cb5a6e..119d82552 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java @@ -17,6 +17,8 @@ import java.util.regex.Pattern; +import com.mycila.maven.plugin.license.util.StringUtils; + /** * The HeaderDefinition class defines what is needed to output a header text into the of the given file * type and what is needed to match the first line as well as the last line of a previous header of the given file @@ -34,6 +36,7 @@ public final class HeaderDefinition { private Pattern skipLinePattern; private Pattern firstLineDetectionPattern; + private Pattern otherLineDetectionPattern; private Pattern lastLineDetectionPattern; private Boolean isMultiline; @@ -52,6 +55,7 @@ public final class HeaderDefinition { * @param skipLinePattern The pattern of lines to skip before being allowed to output this header or null * if it can be outputted from the line of the file. * @param firstLineDetectionPattern The pattern to detect the first line of a previous header. + * @param otherLineDetectionPattern The pattern to detect any following line of a previous header. * @param lastLineDetectionPattern The pattern to detect the last line of a previous header. * @throws IllegalArgumentException If the type name is null. */ @@ -61,6 +65,32 @@ public HeaderDefinition(String type, String skipLinePattern, String firstLineDetectionPattern, String lastLineDetectionPattern, boolean allowBlankLines, boolean isMultiline, boolean padLines) { + this(type, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, null, lastLineDetectionPattern, allowBlankLines, isMultiline, padLines); + } + + /** + * Constructs a new HeaderDefinition object with every header definition properties. + * + * @param type The type name for this header definition. + * @param firstLine The string to output before the content of the first line of this header. + * @param beforeEachLine The string to output before the content of each line of this header (except + * firstLine and endLine). + * @param endLine The string to output before the content of the last line of this header. + * @param afterEachLine The string to output after the content of each line of this header (except + * firstLine and endLine). + * @param skipLinePattern The pattern of lines to skip before being allowed to output this header or null + * if it can be outputted from the line of the file. + * @param firstLineDetectionPattern The pattern to detect the first line of a previous header. + * @param otherLineDetectionPattern The pattern to detect any following line of a previous header. + * @param lastLineDetectionPattern The pattern to detect the last line of a previous header. + * @throws IllegalArgumentException If the type name is null. + */ + public HeaderDefinition(String type, + String firstLine, String beforeEachLine, + String endLine, String afterEachLine, + String skipLinePattern, + String firstLineDetectionPattern, String otherLineDetectionPattern, String lastLineDetectionPattern, + boolean allowBlankLines, boolean isMultiline, boolean padLines) { this(type); this.firstLine = firstLine; this.beforeEachLine = beforeEachLine; @@ -68,6 +98,7 @@ public HeaderDefinition(String type, this.afterEachLine = afterEachLine; this.skipLinePattern = compile(skipLinePattern); this.firstLineDetectionPattern = compile(firstLineDetectionPattern); + this.otherLineDetectionPattern = compile(otherLineDetectionPattern); this.lastLineDetectionPattern = compile(lastLineDetectionPattern); this.allowBlankLines = allowBlankLines; this.isMultiline = isMultiline; @@ -141,12 +172,30 @@ public boolean isSkipLine(String line) { * Tells if the given content line is the first line of a possible header of this definition kind. * * @param line The line to test. - * @return true if the first line of a header have been recognized or false. + * @return true if the first line of a header has been recognized or false. */ public boolean isFirstHeaderLine(String line) { return firstLineDetectionPattern != null && line != null && firstLineDetectionPattern.matcher(line).matches(); } - + + /** + * Tells if the given content line is another than the first line of a possible header of this definition kind. + * + * @param line The line to test. + * @return true if another than the first line of a header has been recognized or false. + */ + public boolean isOtherHeaderLine(String line) { + if (line == null) { + return false; + } else if (otherLineDetectionPattern != null) { + return otherLineDetectionPattern.matcher(line).matches(); + } else if ("".equals(StringUtils.rtrim(beforeEachLine)) && !isMultiLine()) { + return line.startsWith(beforeEachLine); + } else { + return line.startsWith(StringUtils.rtrim(beforeEachLine)); + } + } + /** * Tells if the given content line is the last line of a possible header of this definition kind. * @@ -209,6 +258,7 @@ public void validate() { check("endLine", this.endLine); check("afterEachLine", this.afterEachLine); check("firstLineDetectionPattern", this.firstLineDetectionPattern); + // other line detection pattern can be null check("lastLineDetectionPattern", this.lastLineDetectionPattern); check("isMultiline", this.isMultiline); check("allowBlankLines", this.allowBlankLines); diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java index a7bd6efe5..ac4bbf1d6 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java @@ -179,7 +179,7 @@ private boolean hasHeader() { foundEnd = true; } else { - while ((line = fileContent.nextLine()) != null && line.startsWith(before)) { + while ((line = fileContent.nextLine()) != null && headerDefinition.isOtherHeaderLine(line)) { inPlaceHeader.append(line.toLowerCase()); if (headerDefinition.isMultiLine() && headerDefinition.isLastHeaderLine(line)) { foundEnd = true; diff --git a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java index 6e9711807..0fdc830ed 100755 --- a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java +++ b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java @@ -101,4 +101,33 @@ public void test_parsing_xml6() throws Exception { assertEquals(parser.getBeginPosition(), 45); assertEquals(parser.getEndPosition(), 864); } + + @Test + public void test_parsing_java1() throws Exception { + HeaderParser parser = getCustomJavadocHeaderParser("src/test/resources/doc/java1.txt"); + assertTrue(parser.gotAnyHeader()); + assertEquals("package", parser.getFileContent().getContent().substring(parser.getEndPosition()).trim().substring(0, 7)); + } + + @Test + public void test_parsing_java2() throws Exception { + HeaderParser parser = getCustomJavadocHeaderParser("src/test/resources/doc/java2.txt"); + assertTrue(parser.gotAnyHeader()); + assertEquals("package", parser.getFileContent().getContent().substring(parser.getEndPosition()).trim().substring(0, 7)); + } + + @Test + public void test_parsing_java3() throws Exception { + HeaderParser parser = getCustomJavadocHeaderParser("src/test/resources/doc/java3.txt"); + assertTrue(parser.gotAnyHeader()); + assertEquals("package", parser.getFileContent().getContent().substring(parser.getEndPosition()).trim().substring(0, 7)); + } + + private HeaderParser getCustomJavadocHeaderParser(final String fileName) { + final HeaderDefinition headerDefinition = new HeaderDefinition( + "javadoc2_style", "/**", " ** ", " **/", "", null, "^\\s*/\\*.*$", "^\\s*\\*.*$", "^.*\\*/\\s*$", false, true, false); + return new HeaderParser(new FileContent(new File(fileName), System.getProperty("file.encoding")), headerDefinition, new String[]{"copyright"}); + } + + } diff --git a/license-maven-plugin/src/test/resources/doc/java1.txt b/license-maven-plugin/src/test/resources/doc/java1.txt new file mode 100644 index 000000000..fd20c720f --- /dev/null +++ b/license-maven-plugin/src/test/resources/doc/java1.txt @@ -0,0 +1,4 @@ +/*********************** + ** Copyright ACME + **********************/ +package com.mycila.maven.plugin.license.header; diff --git a/license-maven-plugin/src/test/resources/doc/java2.txt b/license-maven-plugin/src/test/resources/doc/java2.txt new file mode 100644 index 000000000..905d6f54c --- /dev/null +++ b/license-maven-plugin/src/test/resources/doc/java2.txt @@ -0,0 +1,4 @@ +/*********************** + * Copyright ACME + **********************/ +package com.mycila.maven.plugin.license.header; diff --git a/license-maven-plugin/src/test/resources/doc/java3.txt b/license-maven-plugin/src/test/resources/doc/java3.txt new file mode 100644 index 000000000..ff8b3cf14 --- /dev/null +++ b/license-maven-plugin/src/test/resources/doc/java3.txt @@ -0,0 +1,4 @@ +/*********************** +* Copyright ACME * +***********************/ +package com.mycila.maven.plugin.license.header; From 8c6c33b59b1e719494f03e581ac33b2387f16131 Mon Sep 17 00:00:00 2001 From: VLCEK Martin Date: Fri, 19 Nov 2021 14:58:35 +0100 Subject: [PATCH 2/4] https://github.com/mathieucarbou/license-maven-plugin/issues/242: fix javadoc --- .../license-plugin-report.xml | 20 ++--- .../license-plugin-report.xml | 4 +- .../license-plugin-report.xml | 84 +++++++++---------- .../4.2-SNAPSHOT/license-plugin-report.xml | 2 +- .../license/header/HeaderDefinition.java | 1 - 5 files changed, 55 insertions(+), 56 deletions(-) diff --git a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml index 5aa72b25c..d533de190 100644 --- a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml @@ -1,16 +1,16 @@ - + - - - - - - - - - + + + + + + + + + diff --git a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml index 1722927a1..d01ad29f6 100644 --- a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml @@ -1,9 +1,9 @@ - + - + diff --git a/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml index 21f112765..9a5168757 100644 --- a/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml @@ -1,48 +1,48 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml index 590f05fdc..dbce6bf9a 100644 --- a/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml @@ -1,5 +1,5 @@ - + diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java index 119d82552..48bfb9c80 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java @@ -55,7 +55,6 @@ public final class HeaderDefinition { * @param skipLinePattern The pattern of lines to skip before being allowed to output this header or null * if it can be outputted from the line of the file. * @param firstLineDetectionPattern The pattern to detect the first line of a previous header. - * @param otherLineDetectionPattern The pattern to detect any following line of a previous header. * @param lastLineDetectionPattern The pattern to detect the last line of a previous header. * @throws IllegalArgumentException If the type name is null. */ From bbcf1ee1dc0bf42c744366be864b2a5adedd51cf Mon Sep 17 00:00:00 2001 From: VLCEK Martin Date: Fri, 19 Nov 2021 15:05:53 +0100 Subject: [PATCH 3/4] Revert "https://github.com/mathieucarbou/license-maven-plugin/issues/242: fix javadoc" This reverts commit 8c6c33b59b1e719494f03e581ac33b2387f16131. --- .../license-plugin-report.xml | 20 ++--- .../license-plugin-report.xml | 4 +- .../license-plugin-report.xml | 84 +++++++++---------- .../4.2-SNAPSHOT/license-plugin-report.xml | 2 +- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml index d533de190..5aa72b25c 100644 --- a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-git/license-plugin-report.xml @@ -1,16 +1,16 @@ - + - - - - - - - - - + + + + + + + + + diff --git a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml index d01ad29f6..1722927a1 100644 --- a/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-maven-plugin-svn/license-plugin-report.xml @@ -1,9 +1,9 @@ - + - + diff --git a/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml index 9a5168757..21f112765 100644 --- a/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-maven-plugin/license-plugin-report.xml @@ -1,48 +1,48 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml b/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml index dbce6bf9a..590f05fdc 100644 --- a/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml +++ b/docs/reports/4.2-SNAPSHOT/license-plugin-report.xml @@ -1,5 +1,5 @@ - + From b207766f3f3d4b25e586a277a63ea57d70ffba0b Mon Sep 17 00:00:00 2001 From: VLCEK Martin Date: Fri, 19 Nov 2021 15:31:19 +0100 Subject: [PATCH 4/4] fix docs: * In maven/xml we need xml:space="preserve" to prevent trimming of text values. * The XML attribute is multiline, not isMultiline. * inline styles are only available in version 4.2 - we should note this, as the suggested version is 4.1. * the inline example does only work, if the inline style name is lowercase. code fixed in previous PR. --- docs/index.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/index.md b/docs/index.md index f7b3c3c77..a6128af79 100644 --- a/docs/index.md +++ b/docs/index.md @@ -538,14 +538,14 @@ In license-maven-plugin, each header style is defined by patterns to detect it a /** - * - */ + * + */ (\s|\t)*/\*.*$ .*\*/(\s|\t)*$ false - true + true false @@ -558,14 +558,14 @@ And for XML: - + ]]> $]]> (\s|\t)*$]]> false - true + true false @@ -582,12 +582,12 @@ This page will show you how you can define extended header definitions to fit yo #region LicenseEOL/** - * - */EOL#endregion + * + */EOL#endregion #region.*^EOL/\*\*.*$ \*/EOL#endregion" true - true + true ``` @@ -595,7 +595,7 @@ This page will show you how you can define extended header definitions to fit yo * The `EOL` string will be replaced with the proper end of line depending the file format your are processing. * We also have defined the _skipLine_ attribute to skip the region tags (which starts with a '#') * `allowBlankLines` allows you to define if this header style supports blank lines in it or not. In example, in XML headers, you could have blank lines after the because XML delimiters delimit a multiline block. When you work with script style comments like in Ruby, Porperties files, the # character delimit a comment for only one line. So when you create the header, for it to be uniform, you place # on each line. So allowBlankLines will be false. -* `isMultiline` specifies if your header has tokens to delimit a multiline comment of if the tokens are a one-line comment. I.E.: XML style comments are multiline whereas script style comment where each line starts with # are not multiline +* `multiline` specifies if your header has tokens to delimit a multiline comment of if the tokens are a one-line comment. I.E.: XML style comments are multiline whereas script style comment where each line starts with # are not multiline You now have to add this new header definition file to the plugin configuration. It is done as the following in your pom: @@ -637,6 +637,8 @@ And it should generate headers like: __Inline styles__ +(since version 4.2) + This is also possible to configure new header style inline within the POM without using external files. This is a useful feature for module inheritance. The following example will redefine the header file for text files: @@ -653,7 +655,7 @@ The following example will redefine the header file for text files: SMILEY_STYLE :( - ( + ( :( \:\( \:\(