Author: brianf
Date: Wed Jun 13 20:03:44 2007
New Revision: 547099
URL: http://svn.apache.org/viewvc?view=rev&rev=547099
Log:
MENFORCER-7: added evaluateBeanshell rule.
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EvaluateBeanshell.java
maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestEvaluateBeanshell.java
Modified:
maven/plugins/trunk/maven-enforcer-plugin/pom.xml
maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt
maven/plugins/trunk/maven-enforcer-plugin/src/site/site.xml
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestDefaultEnforcementRuleHelper.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=547099&r1=547098&r2=547099
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/pom.xml Wed Jun 13 20:03:44 2007
@@ -116,5 +116,10 @@
<artifactId>maven-enforcer-rule-api</artifactId>
<version>1.0-alpha-1</version>
</dependency>
+ <dependency>
+ <groupId>org.beanshell</groupId>
+ <artifactId>bsh</artifactId>
+ <version>2.0b4</version>
+ </dependency>
</dependencies>
</project>
Modified: maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml?view=diff&rev=547099&r1=547098&r2=547099
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/it/pom.xml Wed Jun 13
20:03:44 2007
@@ -28,16 +28,21 @@
<version>2.0.8</version>
<message>You need 2.0.8!</message>
</requireMavenVersion>
-
<!--<myCustomRule implementation="org.apache.maven.shared.rule.myCustomRule">
-
<shouldIfail>false</shouldIfail>
-
</myCustomRule>-->
-
<requireOS><family>!tandem</family></requireOS>
+ <myCustomRule
implementation="org.apache.maven.shared.rule.myCustomRule">
+
<shouldIfail>true</shouldIfail>
+ </myCustomRule>
+ <requireOS>
+
<family>!tandem</family>
+ </requireOS>
+
<evaluateBeanshell>
+
<condition>rti.getApplicationVersion().getMajorVersion() == 2;</condition>
+
</evaluateBeanshell>
</rules>
</configuration>
<goals>
<goal>enforce-once</goal>
</goals>
-
+
</execution>
</executions>
</plugin>
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EvaluateBeanshell.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EvaluateBeanshell.java?view=auto&rev=547099
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EvaluateBeanshell.java
(added)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/EvaluateBeanshell.java
Wed Jun 13 20:03:44 2007
@@ -0,0 +1,131 @@
+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.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.execution.RuntimeInformation;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+import
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.util.StringUtils;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+
+/**
+ * @author hugonnem Rule for Maven Enforcer using Beanshell
+ * to evaluate a conditional expression
+ *
+ */
+public class EvaluateBeanshell
+ implements EnforcerRule
+{
+
+ /**
+ * Beanshell interpreter
+ */
+ private static final Interpreter bsh = new Interpreter();
+
+ /**
+ * The condition to be evaluated.
+ *
+ * @parameter
+ * @required
+ */
+ public String condition;
+
+ /**
+ * The message to be printed in case the condition
+ * returns <b>true</b>
+ *
+ * @required
+ * @parameter
+ */
+ public String message;
+
+ public void execute( EnforcerRuleHelper helper )
+ throws EnforcerRuleException
+ {
+ Log log = helper.getLog();
+
+ try
+ {
+ // get the various expressions out of the
+ // helper.
+ MavenProject project = (MavenProject) helper.evaluate(
"${project}" );
+ MavenSession session = (MavenSession) helper.evaluate(
"${session}" );
+ String target = (String) helper.evaluate(
"${project.build.directory}" );
+ String artifactId = (String) helper.evaluate(
"${project.artifactId}" );
+ ArtifactResolver resolver = (ArtifactResolver)
helper.getComponent( ArtifactResolver.class );
+ RuntimeInformation rti = (RuntimeInformation) helper.getComponent(
RuntimeInformation.class );
+
+ log.debug( "Retrieved Target Folder: " + target );
+ log.debug( "Retrieved ArtifactId: " + artifactId );
+ log.debug( "Retrieved Project: " + project );
+ log.debug( "Retrieved RuntimeInfo: " + rti );
+ log.debug( "Retrieved Session: " + session );
+ log.debug( "Retrieved Resolver: " + resolver );
+
+ log.debug( "Echo condition : " + this.condition );
+ // Evaluate condition within Plexus Container
+ String script = (String) helper.evaluate( this.condition );
+ log.debug( "Echo script : " + script );
+ if ( !evaluateCondition( script, log ) )
+ {
+ if ( StringUtils.isEmpty( message ) )
+ {
+ message = "The expression \"" + condition + "\" is not
true.";
+ }
+ throw new EnforcerRuleException( this.message );
+ }
+ }
+ catch ( Exception e )
+ {
+ throw new EnforcerRuleException( "Unable to lookup a component", e
);
+ }
+ }
+
+ /**
+ * Evaluate expression using Beanshell
+ *
+ * @param script the expression to be evaluated
+ * @param log the logger
+ * @return boolean the evaluation of the expression
+ */
+ protected boolean evaluateCondition( String script, Log log )
+ {
+ Boolean evaluation = Boolean.FALSE;
+ try
+ {
+ evaluation = (Boolean) bsh.eval( script );
+ log.debug( "Echo evaluating : " + evaluation );
+ }
+ catch ( EvalError ex )
+ {
+ log.warn( "Couldn't evaluate condition: " + script, ex );
+ }
+ return evaluation.booleanValue();
+ }
+
+}
\ No newline at end of file
Modified:
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java?view=diff&rev=547099&r1=547098&r2=547099
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
(original)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/RequireProperty.java
Wed Jun 13 20:03:44 2007
@@ -72,7 +72,7 @@
Object propValue = null;
try
{
- propValue = (String)helper.evaluate("${" + property + "}");
+ propValue = helper.evaluate("${" + property + "}");
}
catch (ExpressionEvaluationException eee)
{
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt?view=auto&rev=547099
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt
(added)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/evaluateBeanshell.apt
Wed Jun 13 20:03:44 2007
@@ -0,0 +1,79 @@
+~~ 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.
+
+ ------
+ Beanshell
+ ------
+ Brian Fox
+ ------
+ June 2007
+ ------
+
+Beanshell
+
+ This rule can execute a beanshell script and evaluate the result.
+
+
+ The following parameters are supported by this rule:
+
+ * condition - the beanshell statement to evaluate.
+
+ * message - an optional message to the user if the rule fails.
+
+ []
+
+
+ Sample Plugin Configuration:
+
++---+
+<project>
+ [...]
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>enforce-beanshell</id>
+ <goals>
+ <goal>enforce-once</goal>
+ </goals>
+ <configuration>
+ <rules>
+ <evaluateBeanshell>
+ <condition>${project.artifactId} == foo</condition>
+ </evaluateBeanshell>
+ </rules>
+ <fail>true</fail>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ [...]
+</project>
++---+
+
+ The condition can be a complex script or a simple expression. As long as it
results in True, the rule will succeed. This means code can be executed as long
as the last line results in true.
+
++---+
+ <evaluateBeanshell>
+ <condition>for (int i = 0;i!=10;i++){print ("Hello World
"+i);};1==1</condition>
+ </evaluateBeanshell>
++---+
\ No newline at end of file
Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt?view=diff&rev=547099&r1=547098&r2=547099
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt
(original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/site/apt/rules/index.apt Wed
Jun 13 20:03:44 2007
@@ -35,4 +35,6 @@
* {{{requireProperty.html}requireProperty}} - enforces the existence
and values of properties.
+ * {{{evaluateBeanshell.html}evaluateBeanshell}} - evaluates a beanshell
script.
+
You may also create and inject your own custom rules by following the
{{{http://maven.apache.org/shared/maven-enforcer-rule-api/writing-a-custom-rule.html}maven-enforcer-rule-api}}
instructions.
Modified: maven/plugins/trunk/maven-enforcer-plugin/src/site/site.xml
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/site/site.xml?view=diff&rev=547099&r1=547098&r2=547099
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/site/site.xml (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/site/site.xml Wed Jun 13
20:03:44 2007
@@ -32,6 +32,7 @@
<item name="RequireJavaVersion"
href="rules/requireJavaVersion.html"/>
<item name="RequireMavenVersion"
href="rules/requireMavenVersion.html"/>
<item name="RequireOS" href="rules/requireOS.html"/>
+ <item name="EvaluateBeanshell" href="rules/evaluateBeanshell.html"/>
<item name="Version Range Specification"
href="rules/versionRanges.html"/>
</menu>
<menu name="Custom Rules">
Modified:
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java?view=diff&rev=547099&r1=547098&r2=547099
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java
(original)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/MockProject.java
Wed Jun 13 20:03:44 2007
@@ -183,7 +183,7 @@
public Artifact getArtifact()
{
- return null;
+ return artifact;
}
public void setArtifact( Artifact artifact )
Modified:
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestDefaultEnforcementRuleHelper.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestDefaultEnforcementRuleHelper.java?view=diff&rev=547099&r1=547098&r2=547099
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestDefaultEnforcementRuleHelper.java
(original)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestDefaultEnforcementRuleHelper.java
Wed Jun 13 20:03:44 2007
@@ -22,8 +22,6 @@
import junit.framework.TestCase;
import org.apache.maven.execution.RuntimeInformation;
-import org.apache.maven.plugin.logging.Log;
-import org.apache.maven.plugin.logging.SystemStreamLog;
import
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
@@ -37,7 +35,6 @@
public void testHelper()
throws ComponentLookupException, ExpressionEvaluationException
{
- Log log = new SystemStreamLog();
DefaultEnforcementRuleHelper helper = (DefaultEnforcementRuleHelper)
EnforcerTestUtils.getHelper();
assertNotNull( helper.getLog() );
Added:
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestEvaluateBeanshell.java
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestEvaluateBeanshell.java?view=auto&rev=547099
==============================================================================
---
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestEvaluateBeanshell.java
(added)
+++
maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestEvaluateBeanshell.java
Wed Jun 13 20:03:44 2007
@@ -0,0 +1,64 @@
+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 junit.framework.TestCase;
+
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ *
+ * @author hugonnem
+ */
+public class TestEvaluateBeanshell
+ extends TestCase
+{
+
+ public void testRule()
+ throws EnforcerRuleException
+ {
+ MockProject project = new MockProject();
+ project.setProperty( "env", "\"This is a test.\"" );
+ EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
+
+ EvaluateBeanshell rule = new EvaluateBeanshell();
+ // this property should not be set
+ rule.condition = "${env} == \"This is a test.\"";
+ rule.message = "We have a variable : ${env}";
+
+ rule.execute( helper );
+
+
+ // this property should be set by the surefire
+ // plugin
+ rule.condition = "${env} == null";
+ try
+ {
+ rule.execute( helper );
+ fail( "Expected an exception." );
+ }
+ catch ( EnforcerRuleException e )
+ {
+ System.out.println("Caught expected
exception:"+e.getLocalizedMessage());
+ }
+ }
+
+}