Fix test scope skipping

This commit is contained in:
Stanislav Ochotnicky 2011-09-08 14:31:03 +02:00
parent 33b0a5fcd6
commit 61f66c0070
6 changed files with 267 additions and 13 deletions

View File

@ -1,14 +1,14 @@
From 8ded699d85fd30717fbdd09a9238f309386747db Mon Sep 17 00:00:00 2001 From 97ddab6cd1a948294176f851e37a48b5bc036c73 Mon Sep 17 00:00:00 2001
From: Stanislav Ochotnicky <sochotnicky@redhat.com> From: Stanislav Ochotnicky <sochotnicky@redhat.com>
Date: Tue, 12 Oct 2010 16:56:50 +0200 Date: Tue, 30 Aug 2011 11:41:39 +0200
Subject: [PATCH 1/2] Add plugin-api deps Subject: [PATCH 1/4] Add plugin-api deps
--- ---
maven-plugin-api/pom.xml | 5 +++++ maven-plugin-api/pom.xml | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-) 1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/maven-plugin-api/pom.xml b/maven-plugin-api/pom.xml diff --git a/maven-plugin-api/pom.xml b/maven-plugin-api/pom.xml
index 621f8d6..ca98242 100644 index 8967754..0f72aa5 100644
--- a/maven-plugin-api/pom.xml --- a/maven-plugin-api/pom.xml
+++ b/maven-plugin-api/pom.xml +++ b/maven-plugin-api/pom.xml
@@ -51,6 +51,11 @@ under the License. @@ -51,6 +51,11 @@ under the License.
@ -24,5 +24,5 @@ index 621f8d6..ca98242 100644
<build> <build>
-- --
1.7.4 1.7.6

View File

@ -1,7 +1,7 @@
From 2f1e46675a26996103ba9c96ae62d3b5876bdba8 Mon Sep 17 00:00:00 2001 From e1b80440159b03b397cba740a7949f8be412fef5 Mon Sep 17 00:00:00 2001
From: Stanislav Ochotnicky <sochotnicky@redhat.com> From: Stanislav Ochotnicky <sochotnicky@redhat.com>
Date: Tue, 18 Jan 2011 11:09:32 +0100 Date: Tue, 30 Aug 2011 11:42:32 +0200
Subject: [PATCH 2/2] Use custom resolver Subject: [PATCH 2/4] Use custom resolver
WorkspaceReader is used to resolve main dependencies, but we had to WorkspaceReader is used to resolve main dependencies, but we had to
add this resolve to ProjectModelResolver and DefaultModelResolver as add this resolve to ProjectModelResolver and DefaultModelResolver as
@ -220,5 +220,5 @@ index c97095f..ef0edc2 100644
} }
-- --
1.7.4 1.7.6

View File

@ -1,7 +1,7 @@
From b84e27c430a321185639096e7a0ca2ab4945bbfa Mon Sep 17 00:00:00 2001 From 053974e55c21a372e544ba23f5eca6c864cc283c Mon Sep 17 00:00:00 2001
From: Stanislav Ochotnicky <sochotnicky@redhat.com> From: Stanislav Ochotnicky <sochotnicky@redhat.com>
Date: Mon, 22 Aug 2011 16:11:57 +0200 Date: Tue, 30 Aug 2011 11:43:01 +0200
Subject: [PATCH] Use utf-8 source encoding Subject: [PATCH 3/4] Use utf-8 source encoding
--- ---
pom.xml | 1 + pom.xml | 1 +

View File

@ -0,0 +1,156 @@
From d3c8d75d197ac8494c87f26ea63733d4f5163c7f Mon Sep 17 00:00:00 2001
From: Stanislav Ochotnicky <sochotnicky@redhat.com>
Date: Tue, 30 Aug 2011 11:44:42 +0200
Subject: [PATCH 4/4] Fix text scope skipping with maven.test.skip
Previously maven put test dependencies into dependency graph even when
"maven.test.skip" was true, therefore. This patch fixes that with few
caveats:
maven-compat was changed so plugins using old api work like this as
well. We removed test deps from "artifacts" Set in
DefaultArtifactResolver if maven.test.skip system property is found.
We created new DependencySelector. Currently it's in wrong place and
possibly has other problems (never going to get upstreamed like
this). But as a hack this works OK.
Main problem: Is using "maven.test.skip" string literally OK? Also,
literally referencing "test" scope is probably not 100% clean.
---
.../artifact/resolver/DefaultArtifactResolver.java | 17 +++++
.../main/java/org/apache/maven/DefaultMaven.java | 3 +-
.../maven/SkipTestScopeDependencySelector.java | 72 ++++++++++++++++++++
3 files changed, 91 insertions(+), 1 deletions(-)
create mode 100644 maven-core/src/main/java/org/apache/maven/SkipTestScopeDependencySelector.java
diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java
index 2e65979..601ace7 100644
--- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java
+++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java
@@ -460,6 +460,23 @@ public class DefaultArtifactResolver
return result;
}
+ // When we find maven.test.skip property we remove dependencies
+ // with scope "test" from artifact set. Current implementation
+ // of artifacts doesn't implement "remove" method so create a
+ // new object and copy non-test artifacts there
+ if (System.getProperty("maven.test.skip") != null) {
+ Set<Artifact> newArtifacts = new LinkedHashSet<Artifact>();
+ for (Artifact artifact: artifacts)
+ {
+ String scope = artifact.getScope();
+ if (scope == null || !scope.equals("test"))
+ {
+ newArtifacts.add(artifact);
+ }
+ }
+ artifacts = newArtifacts;
+ }
+
// After the collection we will have the artifact object in the result but they will not be resolved yet.
result =
artifactCollector.collect( artifacts, rootArtifact, managedVersions, collectionRequest, source,
diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
index 8fdb6ed..75d1e37 100644
--- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
+++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
@@ -31,6 +31,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
+import org.apache.maven.SkipTestScopeDependencySelector;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.eventspy.internal.EventSpyDispatcher;
@@ -437,7 +438,7 @@ public class DefaultMaven
DependencySelector depFilter =
new AndDependencySelector( new ScopeDependencySelector( "test", "provided" ), new OptionalDependencySelector(),
- new ExclusionDependencySelector() );
+ new ExclusionDependencySelector(), new SkipTestScopeDependencySelector() );
session.setDependencySelector( depFilter );
DependencyGraphTransformer transformer =
diff --git a/maven-core/src/main/java/org/apache/maven/SkipTestScopeDependencySelector.java b/maven-core/src/main/java/org/apache/maven/SkipTestScopeDependencySelector.java
new file mode 100644
index 0000000..60be724
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/SkipTestScopeDependencySelector.java
@@ -0,0 +1,72 @@
+package org.apache.maven;
+
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+
+import org.sonatype.aether.collection.DependencyCollectionContext;
+import org.sonatype.aether.collection.DependencySelector;
+import org.sonatype.aether.graph.Dependency;
+import org.sonatype.aether.RepositorySystemSession;
+
+/**
+ * A dependency selector that filters dependencies with scope "test"
+ * when tests are being skipped.
+ *
+ * @author Stanislav Ochotnicky
+ */
+public class SkipTestScopeDependencySelector
+ implements DependencySelector
+{
+
+ private boolean testSkip;
+
+ /**
+ * Creates a new selector
+ */
+ public SkipTestScopeDependencySelector()
+ {
+ testSkip = System.getProperty("maven.test.skip") != null ? true : false;
+ }
+
+ public boolean selectDependency( Dependency dependency )
+ {
+ if (testSkip && dependency.getScope().equals("test"))
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
+ public DependencySelector deriveChildSelector( DependencyCollectionContext context )
+ {
+ return this;
+ }
+
+ @Override
+ public boolean equals( Object obj )
+ {
+ if ( this == obj )
+ {
+ return true;
+ }
+ else if ( null == obj || !getClass().equals( obj.getClass() ) )
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return 42;
+ }
+
+}
--
1.7.6

View File

@ -0,0 +1,93 @@
package org.sonatype.aether.util.graph.selector;
/*******************************************************************************
* Copyright (c) 2010-2011 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Apache License v2.0 is available at
* http://www.apache.org/licenses/LICENSE-2.0.html
* You may elect to redistribute this code under either of these licenses.
*******************************************************************************/
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.sonatype.aether.collection.DependencyCollectionContext;
import org.sonatype.aether.collection.DependencySelector;
import org.sonatype.aether.graph.Dependency;
/**
* A dependency selector that filters transitive dependencies based on their scope. Direct dependencies are always
* included regardless of their scope. <em>Note:</em> This filter does not assume any relationships between the scopes.
* In particular, the filter is not aware of scopes that logically include other scopes.
*
* @author Benjamin Bentmann
* @see Dependency#getScope()
*/
public class SkipTestScopeDependencySelector
implements DependencySelector
{
/**
* Creates a new selector using the specified includes and excludes.
*
* @param included The set of scopes to include, may be {@code null} or empty to include any scope.
* @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
*/
public SkipTestScopeDependencySelector()
{
}
public boolean selectDependency( Dependency dependency )
{
String scope = dependency.getScope();
if (System.getProperty("maven.test.skip") != null &&
scope == "test")
return false;
else return true;
}
public DependencySelector deriveChildSelector( DependencyCollectionContext context )
{
if ( this.transitive || context.getDependency() == null )
{
return this;
}
return new SkipTestScopeDependencySelector( true, included, excluded );
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
else if ( null == obj || !getClass().equals( obj.getClass() ) )
{
return false;
}
SkipTestScopeDependencySelector that = (SkipTestScopeDependencySelector) obj;
return transitive == that.transitive && included.equals( that.included ) && excluded.equals( that.excluded );
}
@Override
public int hashCode()
{
int hash = 17;
hash = hash * 31 + ( transitive ? 1 : 0 );
hash = hash * 31 + included.hashCode();
hash = hash * 31 + excluded.hashCode();
return hash;
}
}

View File

@ -2,7 +2,7 @@
Name: maven Name: maven
Version: 3.0.3 Version: 3.0.3
Release: 13%{?dist} Release: 14%{?dist}
Summary: Java project management and project comprehension tool Summary: Java project management and project comprehension tool
Group: Development/Tools Group: Development/Tools
@ -36,6 +36,7 @@ Patch150: 0001-Add-plugin-api-deps.patch
Patch151: 0003-Use-utf-8-source-encoding.patch Patch151: 0003-Use-utf-8-source-encoding.patch
# Patch2XX for non-upstreamable patches # Patch2XX for non-upstreamable patches
Patch200: 0002-Use-custom-resolver.patch Patch200: 0002-Use-custom-resolver.patch
Patch201: 0004-Fix-text-scope-skipping-with-maven.test.skip.patch
BuildRequires: maven BuildRequires: maven
BuildRequires: maven-parent BuildRequires: maven-parent
@ -114,6 +115,7 @@ Requires: jpackage-utils
%patch150 -p1 %patch150 -p1
%patch151 -p1 %patch151 -p1
%patch200 -p1 %patch200 -p1
%patch201 -p1
# get custom resolver in place # get custom resolver in place
mkdir -p maven-aether-provider/src/main/java/org/apache/maven/artifact/resolver \ mkdir -p maven-aether-provider/src/main/java/org/apache/maven/artifact/resolver \
@ -332,6 +334,9 @@ install -Dm 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/bash_completion.d/%{name}
%changelog %changelog
* Tue Aug 30 2011 Stanislav Ochotnicky <sochotnicky@redhat.com> - 3.0.3-14
- Fix test scope skipping
* Mon Aug 22 2011 Stanislav Ochotnicky <sochotnicky@redhat.com> - 3.0.3-13 * Mon Aug 22 2011 Stanislav Ochotnicky <sochotnicky@redhat.com> - 3.0.3-13
- Remove unnecessary deps causing problems from lib/ - Remove unnecessary deps causing problems from lib/
- Add utf-8 source encoding patch - Add utf-8 source encoding patch