Author: brianf
Date: Sun Mar 18 20:13:30 2007
New Revision: 519810
URL: http://svn.apache.org/viewvc?view=rev&rev=519810
Log:
Coded OSMojo
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractEnforcer.java
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestOSMojo.java
Modified:
maven/plugins/trunk/maven-enforcer-plugin/pom.xml
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractVersionEnforcer.java
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/OsMojo.java
Modified: maven/plugins/trunk/maven-enforcer-plugin/pom.xml
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/pom.xml?view=diff&rev=519810&r1=519809&r2=519810
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/pom.xml Sun Mar 18 20:13:30 2007
@@ -165,7 +165,7 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
- <version>1.4-alpha-1</version>
+ <version>1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractEnforcer.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractEnforcer.java?view=auto&rev=519810
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractEnforcer.java
(added)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractEnforcer.java
Sun Mar 18 20:13:30 2007
@@ -0,0 +1,44 @@
+package org.apache.maven.plugin.enforcer;
+
+import org.apache.maven.plugin.AbstractMojo;
+
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Brian Fox</a>
+ *
+ */
+public abstract class AbstractEnforcer
+ extends AbstractMojo
+{
+ /**
+ * Flag to fail the build if a version check fails.
+ *
+ * @parameter expression="${enforcer.fail}" default-value="true"
+ */
+ protected boolean fail = true;
+
+ /**
+ * Flag to easily skip all checks
+ *
+ * @parameter expression="${enforcer.skip}" default-value="false"
+ */
+ protected boolean skip = false;
+}
Modified:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractVersionEnforcer.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractVersionEnforcer.java?view=diff&rev=519810&r1=519809&r2=519810
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractVersionEnforcer.java
(original)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractVersionEnforcer.java
Sun Mar 18 20:13:30 2007
@@ -24,7 +24,6 @@
import
org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
-import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.StringUtils;
@@ -34,15 +33,10 @@
*
*/
public abstract class AbstractVersionEnforcer
- extends AbstractMojo
+ extends AbstractEnforcer
{
- /**
- * Flag to fail the build if a version check fails.
- *
- * @parameter expression="${enforcer.fail}" default-value="true"
- */
- private boolean fail = true;
+
public boolean enforceVersion( String variableName, String
requiredVersionRange, ArtifactVersion actualVersion )
throws MojoExecutionException
Modified:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java?view=diff&rev=519810&r1=519809&r2=519810
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java
(original)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EnforceMojo.java
Sun Mar 18 20:13:30 2007
@@ -94,27 +94,29 @@
public void execute()
throws MojoExecutionException
{
- boolean foundVersionToCheck = false;
- if ( StringUtils.isNotEmpty( this.mavenVersion ) )
+ if ( !skip )
{
- foundVersionToCheck = true;
- ArtifactVersion detectedMavenVersion = rti.getApplicationVersion();
- enforceVersion( "Maven", this.mavenVersion, detectedMavenVersion );
- }
+ boolean foundVersionToCheck = false;
+ if ( StringUtils.isNotEmpty( this.mavenVersion ) )
+ {
+ foundVersionToCheck = true;
+ ArtifactVersion detectedMavenVersion =
rti.getApplicationVersion();
+ enforceVersion( "Maven", this.mavenVersion,
detectedMavenVersion );
+ }
- if ( StringUtils.isNotEmpty( this.jdkVersion ) )
- {
- foundVersionToCheck = true;
- ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion(
-
fixJDKVersion( SystemUtils.JAVA_VERSION_TRIMMED ) );
- enforceVersion( "JDK", this.jdkVersion, detectedJdkVersion );
- }
+ if ( StringUtils.isNotEmpty( this.jdkVersion ) )
+ {
+ foundVersionToCheck = true;
+ ArtifactVersion detectedJdkVersion = new
DefaultArtifactVersion(
+
fixJDKVersion( SystemUtils.JAVA_VERSION_TRIMMED ) );
+ enforceVersion( "JDK", this.jdkVersion, detectedJdkVersion );
+ }
- if ( !foundVersionToCheck )
- {
- throw new MojoExecutionException( "There is no version range
specified to be checked." );
+ if ( !foundVersionToCheck )
+ {
+ throw new MojoExecutionException( "There is no version range
specified to be checked." );
+ }
}
-
}
/**
Modified:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/OsMojo.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/OsMojo.java?view=diff&rev=519810&r1=519809&r2=519810
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/OsMojo.java
(original)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/OsMojo.java
Sun Mar 18 20:13:30 2007
@@ -19,9 +19,19 @@
* under the License.
*/
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Set;
+
import org.apache.maven.execution.RuntimeInformation;
-import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.model.Activation;
+import org.apache.maven.model.ActivationOS;
+import org.apache.maven.model.Profile;
import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.profiles.activation.OperatingSystemProfileActivator;
+import org.codehaus.plexus.util.Os;
+import org.codehaus.plexus.util.StringUtils;
/**
* Goal which fails the build if the os isn't the correct version
@@ -31,10 +41,55 @@
* @phase process-sources
*/
public class OsMojo
- extends AbstractMojo
+ extends AbstractEnforcer
{
/**
+ * The OS family type desired<br />
+ * Possible values:<br />
+ * <ul>
+ * <li>dos</li>
+ * <li>mac</li>
+ * <li>netware</li>
+ * <li>os/2</li>
+ * <li>tandem</li>
+ * <li>unix</li>
+ * <li>windows</li>
+ * <li>win9x</li>
+ * <li>z/os</li>
+ * <li>os/400</li>
+ * </ul>
+ *
+ * @parameter expression="${enforcer.os.family}" default-value=null
+ *
+ */
+ private String family;
+
+ /**
+ * Runtime information containing Maven Version.
+ *
+ * @parameter expression="${enforcer.os.name}" default-value=null
+ *
+ */
+ private String name;
+
+ /**
+ * Runtime information containing Maven Version.
+ *
+ * @parameter expression="${enforcer.os.version}" default-value=null
+ *
+ */
+ private String version;
+
+ /**
+ * Runtime information containing Maven Version.
+ *
+ * @parameter expression="${enforcer.os.arch}" default-value=null
+ *
+ */
+ private String arch;
+
+ /**
* Runtime information containing Maven Version.
*
* @parameter
expression="${component.org.apache.maven.execution.RuntimeInformation}"
@@ -42,9 +97,210 @@
* @readonly
*/
protected RuntimeInformation rti;
+
+ /**
+ * Display detected OS information
+ *
+ * @parameter expression="${enforcer.os.display}" default-value=false
+ *
+ */
+ private boolean display = false;
+
+ private Set validFamilies = null;
+
+ public static final String OS_NAME = System.getProperty( "os.name"
).toLowerCase( Locale.US );
+
+ public static final String OS_ARCH = System.getProperty( "os.arch"
).toLowerCase( Locale.US );
+
+ public static final String OS_VERSION = System.getProperty( "os.version"
).toLowerCase( Locale.US );
+
+ public OsMojo()
+ {
+ Set families = new HashSet();
+ families.add( "dos" );
+ families.add( "mac" );
+ families.add( "netware" );
+ families.add( "os/2" );
+ families.add( "tandem" );
+ families.add( "unix" );
+ families.add( "windows" );
+ families.add( "win9x" );
+ families.add( "z/os" );
+ families.add( "os/400" );
+
+ validFamilies = families;
+ }
+
public void execute()
throws MojoExecutionException
{
+ if ( !skip )
+ {
+ if (display)
+ {
+ displayOSInfo();
+ }
+
+ if ( isValidFamily( this.family ) )
+ {
+ this.getLog().info( "Is Allowed:" + isAllowed() );
+ }
+ else
+ {
+ StringBuffer buffer = new StringBuffer( 50 );
+ Iterator iter = validFamilies.iterator();
+ while ( iter.hasNext() )
+ {
+ buffer.append( iter.next() );
+ buffer.append( ", " );
+ }
+ String help = StringUtils.stripEnd( buffer.toString().trim(),
"." );
+ throw new MojoExecutionException( "Invalid Family type used.
Valid family types are: " + help );
+ }
+ }
+ }
+
+ public String determineOsFamily()
+ {
+ Iterator iter = this.getValidFamilies().iterator();
+ while ( iter.hasNext() )
+ {
+ String fam = (String) iter.next();
+ if ( Os.isFamily( fam ) )
+ {
+ return fam;
+ }
+ }
+ return null;
+ }
+
+ public void displayOSInfo()
+ {
+ this.getLog().info( "OS Info: Arch: "+OsMojo.OS_ARCH+" Family:
"+determineOsFamily()+" Name: "+OsMojo.OS_NAME+" Version: "+OsMojo.OS_VERSION );
+ }
+
+ public boolean isAllowed()
+ {
+ OperatingSystemProfileActivator activator = new
OperatingSystemProfileActivator();
+
+ return activator.isActive( createProfile() );
+ }
+
+ private Profile createProfile()
+ {
+ Profile profile = new Profile();
+ profile.setActivation( createActivation() );
+ return profile;
+ }
+
+ private Activation createActivation()
+ {
+ Activation activation = new Activation();
+ activation.setActiveByDefault( false );
+ activation.setOs( createOsBean() );
+ return activation;
+ }
+
+ private ActivationOS createOsBean()
+ {
+ ActivationOS os = new ActivationOS();
+
+ os.setArch( arch );
+ os.setFamily( family );
+ os.setName( name );
+ os.setVersion( version );
+
+ return os;
+ }
+
+ public boolean isValidFamily( String theFamily )
+ {
+
+ return ( theFamily == null || validFamilies.contains( theFamily ) );
+ }
+
+ /**
+ * @return the arch
+ */
+ public String getArch()
+ {
+ return this.arch;
+ }
+
+ /**
+ * @param theArch
+ * the arch to set
+ */
+ public void setArch( String theArch )
+ {
+ this.arch = theArch;
+ }
+
+ /**
+ * @return the family
+ */
+ public String getFamily()
+ {
+ return this.family;
+ }
+
+ /**
+ * @param theFamily
+ * the family to set
+ */
+ public void setFamily( String theFamily )
+ {
+ this.family = theFamily;
+ }
+ /**
+ * @return the name
+ */
+ public String getName()
+ {
+ return this.name;
+ }
+
+ /**
+ * @param theName
+ * the name to set
+ */
+ public void setName( String theName )
+ {
+ this.name = theName;
+ }
+
+ /**
+ * @return the version
+ */
+ public String getVersion()
+ {
+ return this.version;
+ }
+
+ /**
+ * @param theVersion
+ * the version to set
+ */
+ public void setVersion( String theVersion )
+ {
+ this.version = theVersion;
+ }
+
+ /**
+ * @return the validFamilies
+ */
+ public Set getValidFamilies()
+ {
+ return this.validFamilies;
+ }
+
+ /**
+ * @param theValidFamilies
+ * the validFamilies to set
+ */
+ public void setValidFamilies( Set theValidFamilies )
+ {
+ this.validFamilies = theValidFamilies;
}
}
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestOSMojo.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestOSMojo.java?view=auto&rev=519810
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestOSMojo.java
(added)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestOSMojo.java
Sun Mar 18 20:13:30 2007
@@ -0,0 +1,117 @@
+package org.apache.maven.plugin.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 java.util.Iterator;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.codehaus.plexus.util.Os;
+
+/**
+ * Exhaustively check the OS mojo.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Brian Fox</a>
+ *
+ */
+public class TestOSMojo
+ extends TestCase
+{
+ public void testOS()
+ {
+ Os os = new Os();
+
+ OsMojo mojo = new OsMojo();
+ mojo.displayOSInfo();
+
+ Iterator iter = mojo.getValidFamilies().iterator();
+ String validFamily = null;
+ String invalidFamily = null;
+ while (iter.hasNext() && (validFamily == null || invalidFamily ==
null))
+ {
+ String fam = (String) iter.next();
+ if (Os.isFamily( fam ))
+ {
+ validFamily = fam;
+ }
+ else
+ {
+ invalidFamily = fam;
+ }
+ }
+
+ mojo.getLog().info( "Testing Mojo Using Valid Family: "+validFamily+"
Invalid Family: "+invalidFamily);
+
+ mojo.setFamily( validFamily );
+ assertTrue( mojo.isAllowed() );
+
+ mojo.setFamily( invalidFamily);
+ assertFalse( mojo.isAllowed() );
+
+ mojo.setFamily( "!"+invalidFamily);
+ assertTrue( mojo.isAllowed() );
+
+ mojo.setFamily( "junk" );
+ try
+ {
+ mojo.execute();
+ fail( "Expected MojoExecution Exception becuase of invalid family
type" );
+ }
+ catch ( MojoExecutionException e )
+ {
+ mojo.getLog().info( "Caught Expected Exception:" +
e.getLocalizedMessage() );
+ }
+
+ mojo.setFamily( null );
+ mojo.setArch( OsMojo.OS_ARCH );
+ assertTrue( mojo.isAllowed() );
+
+ mojo.setArch( "somecrazyarch" );
+ assertFalse(mojo.isAllowed());
+
+ mojo.setArch( "!somecrazyarch" );
+ assertTrue(mojo.isAllowed());
+
+ mojo.setArch( null );
+
+ mojo.setName( OsMojo.OS_NAME );
+ assertTrue( mojo.isAllowed() );
+
+ mojo.setName( "somecrazyname" );
+ assertFalse(mojo.isAllowed());
+
+ mojo.setName( "!somecrazyname" );
+ assertTrue(mojo.isAllowed());
+
+ mojo.setName( null );
+
+ mojo.setVersion( OsMojo.OS_VERSION );
+ assertTrue( mojo.isAllowed() );
+
+ mojo.setVersion( "somecrazyversion" );
+ assertFalse(mojo.isAllowed());
+
+ mojo.setVersion( "!somecrazyversion" );
+ assertTrue(mojo.isAllowed());
+ }
+
+}