You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2023/01/09 18:09:34 UTC
[maven-enforcer] 01/01: [MENFORCER-458] Move RequireOS to new API
This is an automated email from the ASF dual-hosted git repository.
sjaranowski pushed a commit to branch MENFORCER-458-RequireOS
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git
commit 7b5a3d246decede08d6283ffa5c7e335bd4fcb62
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Sat Jan 7 12:37:20 2023 +0100
[MENFORCER-458] Move RequireOS to new API
---
.../enforcer => enforcer/rules}/RequireOS.java | 135 ++++-----------------
.../apache/maven/enforcer/rules/utils/OSUtil.java | 42 +++++++
enforcer-rules/src/site/apt/requireOS.apt.vm | 33 +++--
.../enforcer => enforcer/rules}/TestRequireOS.java | 64 +++++-----
.../src/it/projects/require-os/pom.xml | 54 +++++++++
.../src/it/projects/require-os/verify.groovy | 21 ++++
.../maven/plugins/enforcer/DisplayInfoMojo.java | 6 +-
7 files changed, 188 insertions(+), 167 deletions(-)
diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireOS.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java
similarity index 68%
rename from enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireOS.java
rename to enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java
index 7f620db..7317cab 100644
--- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireOS.java
+++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireOS.java
@@ -16,19 +16,20 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.maven.plugins.enforcer;
+package org.apache.maven.enforcer.rules;
-import java.util.Iterator;
+import javax.inject.Inject;
+import javax.inject.Named;
-import org.apache.maven.enforcer.rule.api.EnforcerRule;
+import java.util.Objects;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
-import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.enforcer.rules.utils.OSUtil;
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationOS;
import org.apache.maven.model.Profile;
import org.apache.maven.model.profile.activation.ProfileActivator;
-import org.apache.maven.plugin.logging.Log;
-import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.StringUtils;
@@ -38,8 +39,9 @@ import org.codehaus.plexus.util.StringUtils;
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
+@Named("requireOS")
public class RequireOS extends AbstractStandardEnforcerRule {
- private ProfileActivator activator;
+ private final ProfileActivator activator;
/**
* The OS family type desired<br />
@@ -56,69 +58,46 @@ public class RequireOS extends AbstractStandardEnforcerRule {
* <li>z/os</li>
* <li>os/400</li>
* </ul>
- *
- * @see {@link #setFamily(String)}
- * @see {@link #getFamily()}
*/
private String family = null;
/**
* The OS name desired.
- *
- * @see {@link #setName(String)}
- * @see {@link #getName()}
*/
private String name = null;
/**
* The OS version desired.
- *
- * @see {@link #setVersion(String)}
- * @see {@link #getVersion()}
*/
private String version = null;
/**
* The OS architecture desired.
- *
- * @see {@link #setArch(String)}
- * @see {@link #getArch()}
*/
private String arch = null;
/**
* Display detected OS information.
- *
- * @see {@link #setDisplay(boolean)}
- * @see {@link #isDisplay()}
*/
private boolean display = false;
/**
* Instantiates a new RequireOS.
*/
- public RequireOS() {}
-
- // For testing
- RequireOS(ProfileActivator activator) {
- this.activator = activator;
+ @Inject
+ RequireOS(@Named("os") ProfileActivator activator) {
+ this.activator = Objects.requireNonNull(activator);
}
@Override
- public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
+ public void execute() throws EnforcerRuleException {
- displayOSInfo(helper.getLog(), display);
+ displayOSInfo();
if (allParamsEmpty()) {
- throw new EnforcerRuleException("All parameters can not be empty. "
- + "You must pick at least one of (family, name, version, arch) "
- + "or use -Denforcer.os.display=true to see the current OS information.");
- }
-
- try {
- activator = helper.getComponent(ProfileActivator.class, "os");
- } catch (ComponentLookupException e) {
- throw new EnforcerRuleException(e.getMessage());
+ throw new EnforcerRuleError("All parameters can not be empty. "
+ + "You must pick at least one of (family, name, version, arch), "
+ + "you can use mvn enforcer:display-info to see the current OS information.");
}
if (isValidFamily(this.family)) {
@@ -139,32 +118,21 @@ public class RequireOS extends AbstractStandardEnforcerRule {
throw new EnforcerRuleException(message);
}
} else {
- final int minimumBufferSize = 50;
- StringBuilder buffer = new StringBuilder(minimumBufferSize);
- Iterator<?> iter = Os.getValidFamilies().iterator();
- while (iter.hasNext()) {
- buffer.append(iter.next());
- buffer.append(", ");
- }
- String help = StringUtils.stripEnd(buffer.toString().trim(), ".");
- throw new EnforcerRuleException("Invalid Family type used. Valid family types are: " + help);
+ String validFamilies = String.join(",", Os.getValidFamilies());
+ throw new EnforcerRuleError("Invalid Family type used. Valid family types are: " + validFamilies);
}
}
/**
* Log the current OS information.
- *
- * @param log the log
- * @param info the info
*/
- public void displayOSInfo(Log log, boolean info) {
- String string = "OS Info: Arch: " + Os.OS_ARCH + " Family: " + Os.OS_FAMILY + " Name: " + Os.OS_NAME
- + " Version: " + Os.OS_VERSION;
+ private void displayOSInfo() {
+ String string = OSUtil.getOSInfo();
- if (!info) {
- log.debug(string);
+ if (!display) {
+ getLog().debug(string);
} else {
- log.info(string);
+ getLog().info(string);
}
}
@@ -184,12 +152,10 @@ public class RequireOS extends AbstractStandardEnforcerRule {
* @return true if all parameters are empty.
*/
public boolean allParamsEmpty() {
- // CHECKSTYLE_OFF: LineLength
return (StringUtils.isEmpty(family)
&& StringUtils.isEmpty(arch)
&& StringUtils.isEmpty(name)
&& StringUtils.isEmpty(version));
- // CHECKSTYLE_ON: LineLength
}
/**
@@ -258,15 +224,6 @@ public class RequireOS extends AbstractStandardEnforcerRule {
return (StringUtils.isEmpty(theFamily) || Os.getValidFamilies().contains(theFamily));
}
- /**
- * Gets the arch.
- *
- * @return the arch
- */
- public String getArch() {
- return this.arch;
- }
-
/**
* Sets the arch.
*
@@ -276,15 +233,6 @@ public class RequireOS extends AbstractStandardEnforcerRule {
this.arch = theArch;
}
- /**
- * Gets the family.
- *
- * @return the family
- */
- public String getFamily() {
- return this.family;
- }
-
/**
* Sets the family.
*
@@ -294,15 +242,6 @@ public class RequireOS extends AbstractStandardEnforcerRule {
this.family = theFamily;
}
- /**
- * Gets the name.
- *
- * @return the name
- */
- public String getName() {
- return this.name;
- }
-
/**
* Sets the name.
*
@@ -312,15 +251,6 @@ public class RequireOS extends AbstractStandardEnforcerRule {
this.name = theName;
}
- /**
- * Gets the version.
- *
- * @return the version
- */
- public String getVersion() {
- return this.version;
- }
-
/**
* Sets the version.
*
@@ -337,10 +267,6 @@ public class RequireOS extends AbstractStandardEnforcerRule {
this.display = display;
}
- public final boolean isDisplay() {
- return display;
- }
-
@Override
public String getCacheId() {
// return the hashcodes of all the parameters
@@ -359,17 +285,4 @@ public class RequireOS extends AbstractStandardEnforcerRule {
}
return b.toString();
}
-
- @Override
- public boolean isCacheable() {
- // the os is not going to change between projects in the same build.
- return true;
- }
-
- @Override
- public boolean isResultValid(EnforcerRule theCachedRule) {
- // i will always return the hash of the parameters as my id. If my parameters are the same, this
- // rule must always have the same result.
- return true;
- }
}
diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/utils/OSUtil.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/utils/OSUtil.java
new file mode 100644
index 0000000..ee9b7c0
--- /dev/null
+++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/utils/OSUtil.java
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+package org.apache.maven.enforcer.rules.utils;
+
+import org.codehaus.plexus.util.Os;
+
+/**
+ * Common os utility.
+ *
+ * @author Slawomir Jaranowski
+ * @since 3.2.0
+ */
+public class OSUtil {
+
+ /**
+ * OS Information used by Enforcer rules and display Mojo.
+ *
+ * @return an on information
+ */
+ public static String getOSInfo() {
+
+ return String.format(
+ "OS Info - Arch: %s, Family: %s, Name: %s, Version: %s",
+ Os.OS_ARCH, Os.OS_FAMILY, Os.OS_NAME, Os.OS_VERSION);
+ }
+}
diff --git a/enforcer-rules/src/site/apt/requireOS.apt.vm b/enforcer-rules/src/site/apt/requireOS.apt.vm
index cc6804a..85e6dda 100644
--- a/enforcer-rules/src/site/apt/requireOS.apt.vm
+++ b/enforcer-rules/src/site/apt/requireOS.apt.vm
@@ -31,11 +31,11 @@ Require OS Version
The following parameters are supported by this rule:
- * message - an optional message to the user if the rule fails.
+ * <<message>> - an optional message to the user if the rule fails.
- * {{{../apidocs/org/apache/maven/plugins/enforcer/RequireOS.html#arch}arch}} - the cpu architecture.
+ * <<arch>> - the cpu architecture.
- * {{{../apidocs/org/apache/maven/plugins/enforcer/RequireOS.html#family}family}} - the family of OS. Possible families are:
+ * <<family>> - the family of OS. Possible families are:
* dos
@@ -60,16 +60,17 @@ Require OS Version
[]
- * {{{../apidocs/org/apache/maven/plugins/enforcer/RequireOS.html#name}name}} - the name of the OS.
+ * <<name>> - the name of the OS.
- * {{{../apidocs/org/apache/maven/plugins/enforcer/RequireOS.html#version}version}} - the version of the OS.
+ * <<version>> - the version of the OS.
- * {{{../apidocs/org/apache/maven/plugins/enforcer/RequireOS.html#display}display}} - flag to display the detected OS information.
+ * <<display>> - flag to display the detected OS information.
[]
- Family is calculated based on testing against the name string retrieved from the JDK. The name, arch and version values are retrieved from the JDK using the following code:
+ Family is calculated based on testing against the name string retrieved from the JDK.
+ The name, arch and version values are retrieved from the JDK using the following code:
+---+
public static final String OS_NAME = System.getProperty( "os.name" ).toLowerCase( Locale.US );
@@ -78,17 +79,11 @@ Require OS Version
public static final String OS_VERSION = System.getProperty( "os.version" ).toLowerCase( Locale.US );
+---+
-
- Possible arch, name, and version values can be found here:
-
- * {{{http://lopica.sourceforge.net/os.html}lopica.sourceforge.net}}
-
- []
-
+
The various options are considered to be "and'd" together but any number can be specified.
(ie family = windows means windows, but family = windows and arch = x86 means only windows on x86 processors)
- Any parameter may also be used in the negative by prepending a "!" in front of it. For example !dos means everything but dos. (who uses dos anyway?)
+ Any parameter may also be used in the negative by prepending a <<<!>>> in front of it. For example <<<!windows>>> means everything but windows.
Since the various names, versions and architecture values cannot be listed exhaustively, there is an easy way to display the
information for the current system:
@@ -96,10 +91,10 @@ Require OS Version
+---+
mvn enforcer:display-info
...
-[enforcer:display-info]
-Maven Version: 2.0.6
-JDK Version: 1.5.0_11 normalized as: 1.5.0
-OS Info: Arch: x86 Family: windows Name: windows xp Version: 5.1
+[INFO] Maven Version: 3.8.7
+[INFO] JDK Version: 1.8.0_352 normalized as: 1.8.0-352
+[INFO] Java Vendor: Homebrew
+[INFO] OS Info - Arch: x86_64, Family: mac, Name: mac os x, Version: 12.6.1
+---+
Sample Plugin Configuration:
diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireOS.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java
similarity index 62%
rename from enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireOS.java
rename to enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java
index e2ef7f0..e7cfdb7 100644
--- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireOS.java
+++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireOS.java
@@ -16,28 +16,28 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.maven.plugins.enforcer;
+package org.apache.maven.enforcer.rules;
import java.util.Iterator;
-import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
-import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.enforcer.rule.api.EnforcerLogger;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.codehaus.plexus.util.Os;
import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.startsWith;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
/**
* Exhaustively check the OS mojo.
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
-public class TestRequireOS {
+class TestRequireOS {
/**
* Test os.
@@ -48,8 +48,6 @@ public class TestRequireOS {
RequireOS rule = new RequireOS(new OperatingSystemProfileActivator());
- rule.displayOSInfo(log, true);
-
Iterator<String> iter = Os.getValidFamilies().iterator();
String validFamily;
String invalidFamily = null;
@@ -66,66 +64,62 @@ public class TestRequireOS {
log.info("Testing Mojo Using Valid Family: " + validFamily + " Invalid Family: " + invalidFamily);
rule.setFamily(validFamily);
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setFamily(invalidFamily);
- assertFalse(rule.isAllowed());
+ assertThat(rule.isAllowed()).isFalse();
rule.setFamily("!" + invalidFamily);
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setFamily(null);
rule.setArch(Os.OS_ARCH);
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setArch("somecrazyarch");
- assertFalse(rule.isAllowed());
+ assertThat(rule.isAllowed()).isFalse();
rule.setArch("!somecrazyarch");
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setArch(null);
rule.setName(Os.OS_NAME);
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setName("somecrazyname");
- assertFalse(rule.isAllowed());
+ assertThat(rule.isAllowed()).isFalse();
rule.setName("!somecrazyname");
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setName(null);
rule.setVersion(Os.OS_VERSION);
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
rule.setVersion("somecrazyversion");
- assertFalse(rule.isAllowed());
+ assertThat(rule.isAllowed()).isFalse();
rule.setVersion("!somecrazyversion");
- assertTrue(rule.isAllowed());
+ assertThat(rule.isAllowed()).isTrue();
}
@Test
- public void testInvalidFamily() {
- RequireOS rule = new RequireOS();
-
- EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
- helper.getContainer().addComponent(new OperatingSystemProfileActivator(), "os");
+ void testInvalidFamily() {
+ RequireOS rule = new RequireOS(new OperatingSystemProfileActivator());
+ rule.setLog(Mockito.mock(EnforcerLogger.class));
rule.setFamily("junk");
- try {
- rule.execute(helper);
- fail("Expected MojoExecution Exception because of invalid family type");
- } catch (EnforcerRuleException e) {
- assertThat(e.getMessage(), startsWith("Invalid Family type used. Valid family types are: "));
- }
+ assertThatCode(rule::execute)
+ .isInstanceOf(EnforcerRuleError.class)
+ .hasMessageStartingWith("Invalid Family type used. Valid family types are: ");
}
@Test
- public void testId() {
- RequireOS rule = new RequireOS();
- rule.getCacheId();
+ void testId() {
+ RequireOS rule = new RequireOS(new OperatingSystemProfileActivator());
+ rule.setVersion("1.2");
+ assertThat(rule.getCacheId()).isNotEmpty();
}
}
diff --git a/maven-enforcer-plugin/src/it/projects/require-os/pom.xml b/maven-enforcer-plugin/src/it/projects/require-os/pom.xml
new file mode 100644
index 0000000..a0b3f22
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-os/pom.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.enforcer</groupId>
+ <artifactId>require-os-test</artifactId>
+ <version>1.0</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <version>@project.version@</version>
+ <executions>
+ <execution>
+ <id>require-os</id>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ <configuration>
+ <rules>
+ <requireOS>
+ <display>true</display>
+ <name>!XXX-Test-Name</name>
+ </requireOS>
+ </rules>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/require-os/verify.groovy b/maven-enforcer-plugin/src/it/projects/require-os/verify.groovy
new file mode 100644
index 0000000..d030b54
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-os/verify.groovy
@@ -0,0 +1,21 @@
+/*
+ * 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('OS Info - Arch')
+assert buildLog.text.contains('[INFO] Rule 0: org.apache.maven.enforcer.rules.RequireOS executed')
diff --git a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/DisplayInfoMojo.java b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/DisplayInfoMojo.java
index 5e77260..d9ffddd 100644
--- a/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/DisplayInfoMojo.java
+++ b/maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/DisplayInfoMojo.java
@@ -18,6 +18,7 @@
*/
package org.apache.maven.plugins.enforcer;
+import org.apache.maven.enforcer.rules.utils.OSUtil;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
@@ -44,11 +45,12 @@ public class DisplayInfoMojo extends AbstractMojo {
String mavenVersion = session.getSystemProperties().getProperty("maven.version");
String javaVersion = System.getProperty("java.version");
String javaVendor = System.getProperty("java.vendor");
+
getLog().info("Maven Version: " + mavenVersion);
getLog().info("JDK Version: " + javaVersion + " normalized as: "
+ RequireJavaVersion.normalizeJDKVersion(javaVersion));
getLog().info("Java Vendor: " + javaVendor);
- RequireOS os = new RequireOS();
- os.displayOSInfo(getLog(), true);
+
+ getLog().info(OSUtil.getOSInfo());
}
}