[MENFORCER-319] Handle parsing of pom.xml in BanDuplicatePomDependencyVersions.java
authorVaclav Haisman <vhaisman@gmail.com>
Tue, 13 Nov 2018 16:28:36 +0000 (17:28 +0100)
committerEnrico Olivelli <eolivelli@apache.org>
Sun, 27 Jan 2019 20:45:25 +0000 (21:45 +0100)
similarly to how actual Maven model parser handles it to fix handling
of pom.xml files with UTF-8 BOM.

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicatePomDependencyVersions.java
maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/invoker.properties [new file with mode: 0644]
maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/pom.xml [new file with mode: 0644]
maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/verify.groovy [new file with mode: 0644]

index 5eaa09a..36a94ed 100644 (file)
@@ -19,8 +19,8 @@ package org.apache.maven.plugins.enforcer;
  * under the License.\r
  */\r
 \r
+import java.io.FileInputStream;\r
 import java.io.FileNotFoundException;\r
-import java.io.FileReader;\r
 import java.io.IOException;\r
 import java.util.HashMap;\r
 import java.util.HashSet;\r
@@ -66,13 +66,13 @@ public class BanDuplicatePomDependencyVersions
 \r
         // re-read model, because M3 uses optimized model\r
         MavenXpp3Reader modelReader = new MavenXpp3Reader();\r
-        FileReader pomReader = null;\r
+        FileInputStream pomInputStream = null;\r
         Model model;\r
         try\r
         {\r
-            pomReader = new FileReader( project.getFile() );\r
+            pomInputStream = new FileInputStream( project.getFile() );\r
 \r
-            model = modelReader.read( pomReader );\r
+            model = modelReader.read( pomInputStream, false );\r
         }\r
         catch ( FileNotFoundException e )\r
         {\r
@@ -88,7 +88,7 @@ public class BanDuplicatePomDependencyVersions
         }\r
         finally\r
         {\r
-            IOUtil.close( pomReader );\r
+            IOUtil.close( pomInputStream );\r
         }\r
 \r
         // @todo reuse ModelValidator when possible\r
diff --git a/maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/invoker.properties b/maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/invoker.properties
new file mode 100644 (file)
index 0000000..c98ac4c
--- /dev/null
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.buildResult=failure
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/pom.xml b/maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/pom.xml
new file mode 100644 (file)
index 0000000..125f87e
--- /dev/null
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- This file is intenitonally in UTF-8 BOM encoding. Keep it that way. -->
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.enforcer</groupId>
+  <artifactId>ban-pom-dependency-version-utf8-with-bom</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <banDuplicatePomDependencyVersions/>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer152</artifactId>
+      <version>1.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer152</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
+
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/verify.groovy b/maven-enforcer-plugin/src/it/projects/ban-pom-dependency-version-utf8-with-bom/verify.groovy
new file mode 100644 (file)
index 0000000..eb917f9
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+File buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions failed with message:' )
+assert buildLog.text.contains( 'Found 1 duplicate dependency declaration in this project:' )
+assert buildLog.text.contains( '- dependencies.dependency[org.apache.maven.plugins.enforcer.its:menforcer152:jar] ( 2 times )' )
+
+
+
\ No newline at end of file