Skip to content
Browse files

Ignore changes in random subdirectory; allow configurable baseDir

git-svn-id: https://svn.apache.org/repos/asf/maven/sandbox/trunk@1031961 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent c1af0c7 commit bc4b34d5b9039c66e93b5449ab69d303fbdfa31f Daniel Fabulich committed Nov 6, 2010
View
47 ...nges-maven-extension/src/main/java/org/apache/maven/shared/scmchanges/MakeScmChanges.java
@@ -66,9 +66,9 @@
/** Disabled by default; activate via -Dmake.scmChanges=true */
boolean enabled = false;
-
- // TODO configurable baseDir
- // TODO Don't just build from the root because we can't figure out what to do
+
+ /** make.baseDir: Search SCM for modified files in this directory. Defaults to ${project.baseDir} for the root project. */
+ File baseDir;
public void afterProjectsRead( MavenSession session )
throws MavenExecutionException
@@ -82,10 +82,11 @@ public void afterProjectsRead( MavenSession session )
}
- List<ScmFile> changedFiles = getChangedFilesFromScm( session.getTopLevelProject().getBasedir() );
+ List<ScmFile> changedFiles = getChangedFilesFromScm( baseDir );
List<String> includedProjects = new ArrayList<String>();
+ MavenProject topLevelProject = session.getTopLevelProject();
for ( ScmFile changedScmFile : changedFiles )
{
logger.debug( changedScmFile.toString() );
@@ -101,9 +102,9 @@ public void afterProjectsRead( MavenSession session )
continue;
}
- File changedFile = new File( changedScmFile.getPath() );
+ File changedFile = new File( changedScmFile.getPath() ).getAbsoluteFile();
- if ( ignoreRootPom && session.getTopLevelProject().getFile().getAbsoluteFile().equals( changedFile.getAbsoluteFile() ) )
+ if ( ignoreRootPom && topLevelProject.getFile().getAbsoluteFile().equals( changedFile) )
{
continue;
}
@@ -115,6 +116,31 @@ public void afterProjectsRead( MavenSession session )
File projectDirectory = project.getFile().getParentFile();
if ( changedFile.getAbsolutePath().startsWith( projectDirectory.getAbsolutePath() + File.separator ) )
{
+ if (topLevelProject.equals( project )) {
+ // If we include the top level project, then we'll build everything.
+ // We have to be very careful before allowing that to happen.
+
+ // In particular, if the modified file is in a subdirectory X that is not itself
+ // a Maven project, we don't want that one file to cause a full build.
+ // i.e. we ignore changes that are in a random subdirectory.
+
+ // Is the top level project actually in the baseDir?
+ // Sometimes people have sibling child projects, e.g.
+ // <module>../child-project</module>
+ // If the top level project isn't the baseDir, then running the whole build may be rational.
+ if (baseDir.equals(projectDirectory.getAbsoluteFile())) {
+
+ // is the changed file the baseDir or one of its immediate descendants?
+ // That should probably provoke a rebuild.
+ if (!(baseDir.equals( changedFile ) || baseDir.equals( changedFile.getParentFile() ))) {
+ // OK, so the changed file is in some random subdirectory of the baseDir.
+ // Skip it.
+ logger.debug( "Not considering top level project for " + changedFile +
+ " because that would trigger a full rebuild." );
+ continue;
+ }
+ }
+ }
if ( !includedProjects.contains( project ) )
{
logger.debug( "Including " + project );
@@ -132,7 +158,7 @@ public void afterProjectsRead( MavenSession session )
if ( includedProjects.isEmpty() )
throw new MavenExecutionException( "No SCM changes detected; nothing to do!",
- session.getTopLevelProject().getFile() );
+ topLevelProject.getFile() );
MavenExecutionRequest request = session.getRequest();
String makeBehavior = request.getMakeBehavior();
@@ -171,6 +197,13 @@ void readParameters( MavenSession session )
enabled = Boolean.parseBoolean( sessionProps.getProperty( "make.scmChanges", "false" ) );
ignoreUnknown = Boolean.parseBoolean( sessionProps.getProperty( "make.ignoreUnknown", "true" ) );
ignoreRootPom = Boolean.parseBoolean( sessionProps.getProperty( "make.ignoreRootPom", "false" ) );
+
+ String basePath = sessionProps.getProperty( "make.baseDir" );
+ if (basePath != null) {
+ baseDir = new File(basePath).getAbsoluteFile();
+ } else {
+ baseDir = session.getTopLevelProject().getBasedir().getAbsoluteFile();
+ }
}
@SuppressWarnings( "unchecked" )
View
54 ...-maven-extension/src/test/java/org/apache/maven/shared/scmchanges/MakeScmChangesTest.java
@@ -1,5 +1,25 @@
package org.apache.maven.shared.scmchanges;
+/*
+ * 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 static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
@@ -38,6 +58,7 @@
public void setUp()
{
msc = new MakeScmChanges();
+ msc.baseDir = new File("").getAbsoluteFile();
msc.logger = mock(Logger.class);
}
@@ -64,8 +85,9 @@ public void readParameters()
String scmConnection = "foo";
when( scm.getConnection() ).thenReturn( scmConnection );
- MavenProject project = mock( MavenProject.class );
- when( project.getScm() ).thenReturn( scm );
+ MavenProject project = new MavenProject();
+ project.setScm( scm );
+ project.setFile( new File("").getAbsoluteFile() );
when( session.getTopLevelProject() ).thenReturn( project );
@@ -284,4 +306,32 @@ public void nothingToDoBecauseIgnoringMissing() throws MavenExecutionException {
}
+ @Test(expected = MavenExecutionException.class)
+ public void nothingToDoBecauseIgnoringRandomSubdirectory() throws MavenExecutionException {
+ MavenSession session = mock(MavenSession.class);
+
+ msc.enabled = true;
+
+ msc = PowerMockito.spy( msc );
+
+ PowerMockito.doNothing().when( msc ).readParameters( (MavenSession) any() );
+
+ ScmFile changedFile = new ScmFile("foo/pom.xml", ScmFileStatus.MODIFIED);
+
+ List<ScmFile> changedFiles = Arrays.asList( changedFile );
+
+ PowerMockito.doReturn( changedFiles ).when( msc ).getChangedFilesFromScm( (File) any() );
+
+ MavenProject project = new MavenProject();
+
+ project.setFile( new File("pom.xml").getAbsoluteFile() );
+
+ when ( session.getTopLevelProject() ).thenReturn( project);
+
+ when ( session.getProjects() ).thenReturn( Arrays.asList( project ));
+
+ msc.afterProjectsRead( session );
+
+ }
+
}

0 comments on commit bc4b34d

Please sign in to comment.
Something went wrong with that request. Please try again.