You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2020/07/31 12:27:11 UTC

[maven-enforcer] branch master updated: [MENFORCER-338] Along with JavaVersion, allow enforcement of the JavaVendor

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new bdef97a  [MENFORCER-338] Along with JavaVersion, allow enforcement of the JavaVendor
bdef97a is described below

commit bdef97a2561ba2d2effd241649ad30a46c8133d4
Author: Tim <ti...@dpgmedia.nl>
AuthorDate: Fri Jul 31 14:26:55 2020 +0200

    [MENFORCER-338] Along with JavaVersion, allow enforcement of the JavaVendor
    
    Signed-off-by: rfscholte <rf...@apache.org>
---
 .../maven/plugins/enforcer/RequireJavaVendor.java  | 70 ++++++++++++++++++++++
 enforcer-rules/src/site/apt/index.apt              |  2 +
 .../src/site/apt/requireJavaVendor.apt.vm          | 67 +++++++++++++++++++++
 .../src/site/apt/requireJavaVersion.apt.vm         |  2 +-
 .../plugins/enforcer/TestRequireJavaVendor.java    | 67 +++++++++++++++++++++
 5 files changed, 207 insertions(+), 1 deletion(-)

diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java
new file mode 100644
index 0000000..5d2444e
--- /dev/null
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireJavaVendor.java
@@ -0,0 +1,70 @@
+package org.apache.maven.plugins.enforcer;
+
+/*
+ * 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.
+ */
+
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ * This rule checks that the Java vendor is allowed.
+ *
+ * @author Tim Sijstermans
+ * @since 3.0.0
+ */
+public class RequireJavaVendor extends AbstractNonCacheableEnforcerRule
+{
+    private String name;
+
+    @Override
+    public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException
+    {
+        if ( !SystemUtils.JAVA_VENDOR.equals( name ) )
+        {
+            String message = getMessage();
+            String error = "Vendor " + SystemUtils.JAVA_VENDOR + " did not match required vendor " + name;
+            StringBuilder sb = new StringBuilder();
+            if ( message != null )
+            {
+                sb.append( message ).append( System.lineSeparator() );
+            }
+
+            sb.append( error );
+
+            throw new EnforcerRuleException( sb.toString() );
+        }
+    }
+
+    /**
+     * Specify the required name. Some examples are:
+     * Name should be an exact match of the System Property java.vendor, which you can also see with mvn --version
+     *
+     * <ul>
+     * <li><code>AdoptOpenJDK</code> enforces vendor name AdoptOpenJDK </li>
+     * <li><code>Amazon</code> enforces vendor name Amazon </li>
+     * </ul>
+     *
+     * @param name the required name to set
+     */
+    public final void setName( String name )
+    {
+        this.name = name;
+    }
+}
diff --git a/enforcer-rules/src/site/apt/index.apt b/enforcer-rules/src/site/apt/index.apt
index fe43cc9..6a88480 100644
--- a/enforcer-rules/src/site/apt/index.apt
+++ b/enforcer-rules/src/site/apt/index.apt
@@ -61,6 +61,8 @@ Built-In Rules
 
   * {{{./requireFilesSize.html}requireFilesSize}} - enforces that the list of files exists and is within a certain size range.
 
+  * {{{./requireJavaVendor.html}requireJavaVendor}} - enforces the JDK vendor.
+
   * {{{./requireJavaVersion.html}requireJavaVersion}} - enforces the JDK version.
 
   * {{{./requireMavenVersion.html}requireMavenVersion}} - enforces the Maven version.
diff --git a/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm b/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm
new file mode 100644
index 0000000..b10bdd2
--- /dev/null
+++ b/enforcer-rules/src/site/apt/requireJavaVendor.apt.vm
@@ -0,0 +1,67 @@
+~~ 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.    
+ 
+  ------
+  Require Java Vendor
+  ------
+  Tim Sijstermans
+  ------
+  July 2020
+  ------
+
+Require Java Vendor
+
+   This rule enforces a certain Java JDK vendor. The rule uses the full vendor name to verify.
+
+   The following parameters are supported by this rule:
+   
+   * message - an optional message to the user if the rule fails.
+   
+   * name - The name of the allowed JDK vendor.
+ 
+   Sample Plugin Configuration:
+   
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>enforce-java</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireJavaVendor>
+                  <name>AdoptOpenJDK</name>
+                </requireJavaVendor>
+              </rules>    
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+
\ No newline at end of file
diff --git a/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm b/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm
index 4022cca..b7a7251 100644
--- a/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm
+++ b/enforcer-rules/src/site/apt/requireJavaVersion.apt.vm
@@ -92,4 +92,4 @@ OS Info: Arch: x86 Family: windows Name: windows xp Version: 5.1
 </project>
 +---+
   
-  If you would like to enforce a certain vendor string for the JDK, you would use the {{{./requireProperty.html}RequireProperty}} rule, and check the java.vendor property with a regular expression.
+  If you would like to enforce a certain vendor string for the JDK, you would use the {{{./requireJavaVendor.html}RequireJavaVendor}} rule
diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java
new file mode 100644
index 0000000..a3f0927
--- /dev/null
+++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireJavaVendor.java
@@ -0,0 +1,67 @@
+package org.apache.maven.plugins.enforcer;
+
+/*
+ * 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.
+ */
+
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.junit.Test;
+
+/**
+ * The Class TestRequireJavaVendor.
+ *
+ * @author Tim Sijstermans
+ */
+public class TestRequireJavaVendor
+{
+
+    /**
+     * Test Rule: Success case
+     *
+     * @throws EnforcerRuleException the enforcer rule exception
+     */
+    @Test
+    public void settingTheRequiredJavaVendorToSystemVendorShouldNotFail() throws EnforcerRuleException
+    {
+        RequireJavaVendor underTest = new RequireJavaVendor();
+        // Set the required vendor to the current system vendor
+        underTest.setName( SystemUtils.JAVA_VENDOR );
+        final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
+        underTest.execute( helper );
+        // No assert and no expected exception because this test should not fail
+    }
+
+    /**
+     * Test rule: Failing case
+     *
+     * @throws EnforcerRuleException the enforcer rule exception
+     */
+    @Test( expected = EnforcerRuleException.class )
+    public void excludingTheCurrentVendorShouldFail() throws EnforcerRuleException
+    {
+
+        RequireJavaVendor underTest = new RequireJavaVendor();
+        // Set the required vendor to something nonsensical
+        underTest.setName( "..." + SystemUtils.JAVA_VENDOR );
+        final EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
+        underTest.execute( helper );
+        // expect EnforcerRuleException to happen
+    }
+}
\ No newline at end of file