Author: brianf Date: Thu Feb 22 17:55:32 2007 New Revision: 510758 URL: http://svn.apache.org/viewvc?view=rev&rev=510758 Log: intermediate work product.
Added: maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/abstractVersionEnforcer.java maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/enforce.java Modified: maven/sandbox/trunk/plugins/maven-enforcer-plugin/pom.xml maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/JdkMojo.java maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/MavenMojo.java Modified: maven/sandbox/trunk/plugins/maven-enforcer-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-enforcer-plugin/pom.xml?view=diff&rev=510758&r1=510757&r2=510758 ============================================================================== --- maven/sandbox/trunk/plugins/maven-enforcer-plugin/pom.xml (original) +++ maven/sandbox/trunk/plugins/maven-enforcer-plugin/pom.xml Thu Feb 22 17:55:32 2007 @@ -167,5 +167,10 @@ <artifactId>plexus-utils</artifactId> <version>1.4-alpha-1</version> </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.3</version> + </dependency> </dependencies> </project> Modified: maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/JdkMojo.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/JdkMojo.java?view=diff&rev=510758&r1=510757&r2=510758 ============================================================================== --- maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/JdkMojo.java (original) +++ maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/JdkMojo.java Thu Feb 22 17:55:32 2007 @@ -19,8 +19,12 @@ * under the License. */ +import org.apache.commons.lang.SystemUtils; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; /** * Goal which fails the build if the jdk isn't the correct version @@ -30,12 +34,34 @@ * @phase process-sources */ public class JdkMojo - extends AbstractMojo + extends abstractVersionEnforcer { + /** + * Specify the required Version of Maven. + * Some examples are + * <ul> + * <li><code>2.0.4</code> Version 2.0.4</li> + * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li> + * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li> + * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li> + * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li> + * </ul> + * + * @parameter expression="${enforcer.jdk.version}" default-value="" + * @required + */ + private String jdkVersion = null; + /** + * Flag to warn only if the mavenVersion check fails. + * + * @parameter expression="${enforcer.jdk.warn}" default-value="false" + */ + private boolean warn = false; public void execute() - throws MojoExecutionException + throws MojoExecutionException, MojoFailureException { - + ArtifactVersion version = new DefaultArtifactVersion(SystemUtils.JAVA_VERSION_TRIMMED.replace('_','-')); + this.enforceVersion("JDK",this.jdkVersion,version,this.warn); } } Modified: maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/MavenMojo.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/MavenMojo.java?view=diff&rev=510758&r1=510757&r2=510758 ============================================================================== --- maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/MavenMojo.java (original) +++ maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/MavenMojo.java Thu Feb 22 17:55:32 2007 @@ -24,6 +24,7 @@ import org.apache.maven.execution.RuntimeInformation; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; import org.codehaus.plexus.util.StringUtils; @@ -35,7 +36,7 @@ * @phase process-sources */ public class MavenMojo - extends AbstractMojo + extends abstractVersionEnforcer { /** * Used to look up Artifacts in the remote repository. @@ -70,35 +71,9 @@ private boolean warn = false; public void execute() - throws MojoExecutionException + throws MojoExecutionException, MojoFailureException { - if (StringUtils.isEmpty(this.mavenVersion)) - { - throw new MojoExecutionException("MavenVersion can't be empty."); - } - ArtifactVersion version = rti.getApplicationVersion(); - VersionRange vr; - - vr = VersionRange.createFromVersion(this.mavenVersion); - - Log log = this.getLog(); - String msg = "Detected Maven Version: "+ version; - if (vr.containsVersion(version)) - { - log.debug(msg+" is allowed."); - } - else - { - String error = msg+" is not in the allowed range: "+vr; - if (warn) - { - log.warn(error); - } - else - { - throw new MojoExecutionException(error); - } - } + this.enforceVersion("Maven",this.mavenVersion,version,this.warn); } } Added: maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/abstractVersionEnforcer.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/abstractVersionEnforcer.java?view=auto&rev=510758 ============================================================================== --- maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/abstractVersionEnforcer.java (added) +++ maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/abstractVersionEnforcer.java Thu Feb 22 17:55:32 2007 @@ -0,0 +1,54 @@ +/** + * + */ +package org.apache.maven.plugin.enforcer; + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.StringUtils; + +/** + * @author brianf + * + */ +public abstract class abstractVersionEnforcer + extends AbstractMojo +{ + + public void enforceVersion( String variableName, String requiredVersionRange, ArtifactVersion actualVersion, + boolean warn ) + throws MojoExecutionException, MojoFailureException + { + if ( StringUtils.isEmpty( requiredVersionRange ) ) + { + throw new MojoExecutionException( variableName + " version can't be empty." ); + } + + VersionRange vr; + + vr = VersionRange.createFromVersion( requiredVersionRange ); + + Log log = this.getLog(); + String msg = "Detected " + variableName + " Version: " + actualVersion; + if ( vr.containsVersion( actualVersion ) ) + { + log.debug( msg + " is allowed." ); + } + else + { + String error = msg + " is not in the allowed range: " + vr; + if ( warn ) + { + log.warn( error ); + } + else + { + throw new MojoExecutionException( error ); + } + } + } +} Added: maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/enforce.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/enforce.java?view=auto&rev=510758 ============================================================================== --- maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/enforce.java (added) +++ maven/sandbox/trunk/plugins/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/enforce.java Thu Feb 22 17:55:32 2007 @@ -0,0 +1,177 @@ +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 org.apache.commons.lang.SystemUtils; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.execution.RuntimeInformation; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.codehaus.plexus.util.StringUtils; + +/** + * Goal which fails the build if the jdk isn't the correct version + * + * @goal enforce + * @author Brian Fox + * @phase process-sources + */ +public class enforce + extends AbstractMojo +{ + /** + * Used to look up Artifacts in the remote repository. + * + * @parameter expression="${component.org.apache.maven.execution.RuntimeInformation}" + * @required + * @readonly + */ + protected RuntimeInformation rti; + + /** + * Specify the required Version of Maven. Some examples are + * <ul> + * <li><code>2.0.4</code> Version 2.0.4</li> + * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not + * included)</li> + * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li> + * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li> + * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and + * 2.1.1 or higher</li> + * </ul> + * + * @parameter expression="${enforcer.maven.version}" default-value="" + * @required + */ + private String mavenVersion = null; + + /** + * Specify the required Version of JDK. Some examples are + * <ul> + * <li><code>2.0.4</code> Version 2.0.4</li> + * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not + * included)</li> + * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li> + * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li> + * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and + * 2.1.1 or higher</li> + * </ul> + * + * @parameter expression="${enforcer.jdk.version}" default-value="" + * @required + */ + private String jdkVersion = null; + + /** + * Flag to warn only if the mavenVersion check fails. + * + * @parameter expression="${enforcer.warn}" default-value="false" + */ + private boolean warn = false; + + /** + * + */ + public enforce() + { + super(); + // TODO Auto-generated constructor stub + } + + /* + * (non-Javadoc) + * + * @see org.apache.maven.plugin.Mojo#execute() + */ + public void execute() + throws MojoExecutionException, MojoFailureException + { + if ( StringUtils.isNotEmpty( this.mavenVersion ) ) + { + ArtifactVersion version = rti.getApplicationVersion(); + this.enforceVersion( "Maven", this.mavenVersion, version, this.warn ); + } + + if ( StringUtils.isNotEmpty( this.jdkVersion ) ) + { + ArtifactVersion version = new DefaultArtifactVersion(fixJDKVersion(SystemUtils.JAVA_VERSION_TRIMMED)); + + this.enforceVersion( "JDK", fixJDKVersion(this.jdkVersion), version, this.warn ); + } + + } + + public String fixJDKVersion( String jdkVersion ) + { + + /* String token[] = StringUtils.split( jdkVersion.replace( '_', '.' ), "." ); + StringBuffer buffer = new StringBuffer( jdkVersion.length() ); + for ( int i = 0; i <= 2; i++ ) + { + buffer.append( token[i] ); + buffer.append( "." ); + } + + String version = buffer.toString(); + return StringUtils.stripEnd(version,"."); + */ + return jdkVersion; + } + + public void enforceVersion( String variableName, String requiredVersionRange, ArtifactVersion actualVersion, + boolean warn ) + throws MojoExecutionException, MojoFailureException + { + VersionRange vr = null; + try + { + vr = VersionRange.createFromVersionSpec( requiredVersionRange ); + } + catch ( InvalidVersionSpecificationException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Log log = this.getLog(); + String msg = "Detected " + variableName + " Version: " + actualVersion; + if ( vr.containsVersion( actualVersion ) ) + { + log.debug( msg + " is allowed." ); + } + else + { + String error = msg + " is not in the allowed range: " + vr; + if ( warn ) + { + log.warn( error ); + } + else + { + throw new MojoExecutionException( error ); + } + } + } +}