Author: gboue
Date: Tue Sep 20 20:40:57 2016
New Revision: 1761637

URL: http://svn.apache.org/viewvc?rev=1761637&view=rev
Log:
[MENFORCER-204] Add new rule: should be able to make sure that project artifact 
is a Snapshot

Adding a RequireSnapshotVersion rule, which is the opposite of the 
RequireReleaseVersion rule.

Added:
    
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java
   (with props)
    
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm  
 (with props)
    
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java
   (with props)
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties
   (with props)
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml
   (with props)
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/
    
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml
   (with props)
Modified:
    maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt

Added: 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java?rev=1761637&view=auto
==============================================================================
--- 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java
 (added)
+++ 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java
 Tue Sep 20 20:40:57 2016
@@ -0,0 +1,92 @@
+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.maven.artifact.Artifact;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.project.MavenProject;
+import 
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+
+/**
+ * This rule checks that the current project is not a release.
+ */
+public class RequireSnapshotVersion
+    extends AbstractNonCacheableEnforcerRule
+{
+
+    /**
+     * Allows this rule to fail when the parent is defined as a release.
+     */
+    private boolean failWhenParentIsRelease = true;
+
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+
+        MavenProject project = getProject( helper );
+        Artifact artifact = project.getArtifact();
+
+        if ( !artifact.isSnapshot() )
+        {
+            String message = getMessage();
+            StringBuilder sb = new StringBuilder();
+            if ( message != null )
+            {
+                sb.append( message ).append( '\n' );
+            }
+            sb.append( "This project cannot be a release:" ).append( 
artifact.getId() );
+            throw new EnforcerRuleException( sb.toString() );
+        }
+        if ( failWhenParentIsRelease )
+        {
+            Artifact parentArtifact = project.getParentArtifact();
+            if ( parentArtifact != null && !parentArtifact.isSnapshot() )
+            {
+                throw new EnforcerRuleException( "Parent cannot be a release: 
" + parentArtifact.getId() );
+            }
+        }
+
+    }
+
+    private MavenProject getProject( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        try
+        {
+            return (MavenProject) helper.evaluate( "${project}" );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the 
MavenProject: ", eee );
+        }
+    }
+
+    public boolean isFailWhenParentIsRelease()
+    {
+        return failWhenParentIsRelease;
+    }
+
+    public void setFailWhenParentIsRelease( boolean failWhenParentIsRelease )
+    {
+        this.failWhenParentIsRelease = failWhenParentIsRelease;
+    }
+
+}

Propchange: 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireSnapshotVersion.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt?rev=1761637&r1=1761636&r2=1761637&view=diff
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt (original)
+++ maven/enforcer/trunk/enforcer-rules/src/site/apt/index.apt Tue Sep 20 
20:40:57 2016
@@ -75,6 +75,8 @@ Standard Rules
 
   * {{{./requireReleaseVersion.html}requireReleaseVersion}} - enforces that 
the artifact is not a snapshot.
 
+  * {{{./requireSnapshotVersion.html}requireSnapshotVersion}} - enforces that 
the artifact is not a release.
+
   * {{{./requireSameVersions.html}requireSameVersions}} - enforces that 
specific dependencies and/or plugins have the same version.
 
   * {{{./requireUpperBoundDeps.html}requireUpperBoundDeps}} - ensures that 
every (transitive) dependency is resolved to it's specified version or higher.

Added: 
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm?rev=1761637&view=auto
==============================================================================
--- 
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm 
(added)
+++ 
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm 
Tue Sep 20 20:40:57 2016
@@ -0,0 +1,72 @@
+~~ 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 Snapshot Version
+  ------
+  Guillaume Boue
+  ------
+  September 2016
+  ------
+
+Require Snapshot Version
+
+  This rule checks that the current project is not a release.
+
+
+   The following parameters are supported by this rule:
+   
+   * message - an optional message to the user if the rule fails.
+    
+   * failWhenParentIsRelease - if the parent should be checked. Default: true 
+
+   []
+
+   
+  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-no-releases</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSnapshotVersion>
+                  <message>No Releases Allowed!</message>
+                </requireSnapshotVersion>
+              </rules>
+              <fail>true</fail>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+</project>
++---+

Propchange: 
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/enforcer/trunk/enforcer-rules/src/site/apt/requireSnapshotVersion.apt.vm
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java?rev=1761637&view=auto
==============================================================================
--- 
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java
 (added)
+++ 
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java
 Tue Sep 20 20:40:57 2016
@@ -0,0 +1,101 @@
+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.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.plugin.testing.ArtifactStubFactory;
+import org.apache.maven.plugins.enforcer.utils.TestEnforcerRuleUtils;
+import org.apache.maven.project.MavenProject;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+
+/**
+ * Test class for the RequireSnapshotVersion rule.
+ */
+public class TestRequireSnapshotVersion
+{
+
+    private MavenProject project;
+
+    private EnforcerRuleHelper helper;
+
+    private ArtifactStubFactory factory;
+
+    private RequireSnapshotVersion rule;
+
+    @Before
+    public void before()
+    {
+        project = new MockProject();
+        helper = EnforcerTestUtils.getHelper( project );
+        factory = new ArtifactStubFactory();
+        rule = new RequireSnapshotVersion();
+    }
+
+    @Test
+    public void testRequireSnapshot()
+        throws IOException
+    {
+        project.setArtifact( factory.getReleaseArtifact() );
+        TestEnforcerRuleUtils.execute( rule, helper, true );
+
+        project.setArtifact( factory.getSnapshotArtifact() );
+        TestEnforcerRuleUtils.execute( rule, helper, false );
+    }
+
+    @Test
+    public void testWithParentShouldFail()
+        throws IOException
+    {
+        project.setArtifact( factory.getSnapshotArtifact() );
+        rule.setFailWhenParentIsRelease( true );
+
+        MockProject parent = new MockProject();
+        parent.setArtifact( factory.getReleaseArtifact() );
+        project.setParent( parent );
+        TestEnforcerRuleUtils.execute( rule, helper, true );
+
+        parent = new MockProject();
+        parent.setArtifact( factory.getSnapshotArtifact() );
+        project.setParent( parent );
+        TestEnforcerRuleUtils.execute( rule, helper, false );
+    }
+
+    @Test
+    public void testWithParentShouldPass()
+        throws IOException
+    {
+        project.setArtifact( factory.getSnapshotArtifact() );
+        rule.setFailWhenParentIsRelease( false );
+
+        MockProject parent = new MockProject();
+        parent.setArtifact( factory.getReleaseArtifact() );
+        project.setParent( parent );
+        TestEnforcerRuleUtils.execute( rule, helper, false );
+
+        parent = new MockProject();
+        parent.setArtifact( factory.getSnapshotArtifact() );
+        project.setParent( parent );
+        TestEnforcerRuleUtils.execute( rule, helper, false );
+    }
+
+}

Propchange: 
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireSnapshotVersion.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties?rev=1761637&view=auto
==============================================================================
--- 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties
 (added)
+++ 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties
 Tue Sep 20 20:40:57 2016
@@ -0,0 +1,18 @@
+# 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.
+
+invoker.buildResult=failure
\ No newline at end of file

Propchange: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/invoker.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml?rev=1761637&view=auto
==============================================================================
--- 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml
 (added)
+++ 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml
 Tue Sep 20 20:40:57 2016
@@ -0,0 +1,53 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.enforcer</groupId>
+  <artifactId>menforcer-204</artifactId>
+  <version>1.0</version>
+
+  
<description>https://issues.apache.org/jira/browse/MENFORCER-204</description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSnapshotVersion/>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_failure/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml?rev=1761637&view=auto
==============================================================================
--- 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml
 (added)
+++ 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml
 Tue Sep 20 20:40:57 2016
@@ -0,0 +1,53 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.enforcer</groupId>
+  <artifactId>menforcer-204</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  
<description>https://issues.apache.org/jira/browse/MENFORCER-204</description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSnapshotVersion/>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/require-snapshot-version_success/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision


Reply via email to