Skip to content

Commit fa8f7f2

Browse files
[MENFORCER-389] Allow filtering of parent in requireReleaseDeps
1 parent fd574ec commit fa8f7f2

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireReleaseDeps.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
* under the License.
2020
*/
2121

22+
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.List;
25+
import java.util.Optional;
2426
import java.util.Set;
2527

2628
import org.apache.maven.artifact.Artifact;
@@ -43,8 +45,6 @@ public class RequireReleaseDeps
4345
/**
4446
* Allows this rule to execute only when this project is a release.
4547
*
46-
* @parameter
47-
*
4848
* @see {@link #setOnlyWhenRelease(boolean)}
4949
* @see {@link #isOnlyWhenRelease()}
5050
@@ -54,8 +54,6 @@ public class RequireReleaseDeps
5454
/**
5555
* Allows this rule to fail when the parent is defined as a snapshot.
5656
*
57-
* @parameter
58-
*
5957
* @see {@link #setFailWhenParentIsSnapshot(boolean)}
6058
* @see {@link #isFailWhenParentIsSnapshot()}
6159
*/
@@ -98,6 +96,7 @@ public void execute( EnforcerRuleHelper helper )
9896
{
9997
callSuper = true;
10098
}
99+
101100
if ( callSuper )
102101
{
103102
super.execute( helper );
@@ -107,7 +106,17 @@ public void execute( EnforcerRuleHelper helper )
107106
{
108107
project = getProject( helper );
109108
}
109+
110110
Artifact parentArtifact = project.getParentArtifact();
111+
112+
if ( parentArtifact != null )
113+
{
114+
Set<Artifact> artifacts = filterArtifacts( Collections.singleton( parentArtifact ) );
115+
parentArtifact = Optional.ofNullable( artifacts )
116+
.flatMap( s -> s.stream().findFirst() )
117+
.orElse( null );
118+
}
119+
111120
if ( parentArtifact != null && parentArtifact.isSnapshot() )
112121
{
113122
throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );

enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireReleaseDeps.java

+30-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* under the License.
2020
*/
2121

22+
import java.io.IOException;
2223
import java.util.Collections;
2324
import java.util.Set;
2425

@@ -30,12 +31,14 @@
3031
import org.apache.maven.project.ProjectBuildingRequest;
3132
import org.junit.jupiter.api.Test;
3233

34+
import static org.assertj.core.api.Assertions.assertThat;
35+
3336
/**
34-
* The Class TestNoSnapshots.
37+
* The Class TestRequireReleaseDeps.
3538
*
3639
* @author <a href="mailto:[email protected]">Brian Fox</a>
3740
*/
38-
public class TestRequireReleaseDeps
41+
class TestRequireReleaseDeps
3942
{
4043

4144
/**
@@ -44,7 +47,7 @@ public class TestRequireReleaseDeps
4447
* @throws Exception if any occurs
4548
*/
4649
@Test
47-
public void testRule()
50+
void testRule()
4851
throws Exception
4952
{
5053
ArtifactStubFactory factory = new ArtifactStubFactory();
@@ -90,7 +93,7 @@ public void testRule()
9093
}
9194

9295
@Test
93-
public void testWildcardIgnore()
96+
void testWildcardIgnore()
9497
throws Exception
9598
{
9699
RequireReleaseDeps rule = newRequireReleaseDeps();
@@ -112,12 +115,33 @@ public void testWildcardIgnore()
112115
* Test id.
113116
*/
114117
@Test
115-
public void testId()
118+
void testId()
116119
{
117120
RequireReleaseDeps rule = newRequireReleaseDeps();
118-
rule.getCacheId();
121+
assertThat( rule.getCacheId() ).isEqualTo( "0" );
119122
}
120123

124+
@Test
125+
void parentShouldBeExcluded() throws IOException
126+
{
127+
128+
ArtifactStubFactory factory = new ArtifactStubFactory();
129+
MockProject project = new MockProject();
130+
project.setArtifact( factory.getSnapshotArtifact() );
131+
132+
MavenProject parent = new MockProject();
133+
parent.setArtifact( factory.getSnapshotArtifact() );
134+
project.setParent( parent );
135+
136+
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
137+
138+
RequireReleaseDeps rule = newRequireReleaseDeps();
139+
rule.setExcludes( Collections.singletonList( parent.getArtifact().getGroupId() + ":*" ) );
140+
141+
EnforcerRuleUtilsHelper.execute( rule, helper, false );
142+
}
143+
144+
121145
private RequireReleaseDeps newRequireReleaseDeps()
122146
{
123147
return new RequireReleaseDeps()

0 commit comments

Comments
 (0)