Skip to content

Commit

Permalink
[MRELEASE-1007] Rework usage workingDirectory and commonBasedir
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte authored and theit committed Jun 28, 2018
1 parent 2dcc0d3 commit eabcfb8
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,23 @@ public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnviro
"Cannot find Maven executor with id: " + releaseEnvironment.getMavenExecutorId() );
}

File wd =
determineWorkingDirectory( workingDirectory,
releaseDescriptor.getScmRelativePathProjectDirectory() );
mavenExecutor.executeGoals( wd, goals, releaseEnvironment, releaseDescriptor.isInteractive(),
additionalArguments, releaseDescriptor.getPomFileName(), result );
File executionRoot;
String pomFileName;
if ( releaseDescriptor.getPomFileName() != null )
{
File rootPom = new File( workingDirectory, releaseDescriptor.getPomFileName() );
executionRoot = rootPom.getParentFile();
pomFileName = rootPom.getName();
}
else
{
executionRoot = workingDirectory;
pomFileName = null;
}

mavenExecutor.executeGoals( executionRoot, goals, releaseEnvironment,
releaseDescriptor.isInteractive(), additionalArguments,
pomFileName, result );
}
}
catch ( MavenExecutorException e )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,48 +167,18 @@ public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDe
List<MavenProject> reactorProjects )
throws ReleaseExecutionException
{
Path basedir;
try
{
basedir = getCommonBasedir( reactorProjects );
}
catch ( IOException e )
{
throw new ReleaseExecutionException( "Exception occurred while calculating common basedir: "
+ e.getMessage(), e );
}

int parentLevels =
getBaseWorkingDirectoryParentCount( basedir, Paths.get( releaseDescriptor.getWorkingDirectory() ) );
int parentLevels = Paths.get( releaseDescriptor.getPomFileName() ).getNameCount() - 1;

String url = releaseDescriptor.getScmSourceUrl();
url = realignScmUrl( parentLevels, url );

ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
builder.setWorkingDirectory( basedir.toString() );
builder.setWorkingDirectory( releaseDescriptor.getWorkingDirectory() );
builder.setScmSourceUrl( url );

return ReleaseUtils.buildReleaseDescriptor( builder );
}

static Path getCommonBasedir( List<MavenProject> reactorProjects )
throws IOException
{
Path basePath = reactorProjects.get( 0 ).getBasedir().toPath();

for ( MavenProject reactorProject : reactorProjects )
{
Path matchPath = reactorProject.getBasedir().toPath();
while ( !basePath.startsWith( matchPath ) )
{
matchPath = matchPath.getParent();
}
basePath = matchPath;
}

return basePath;
}

public static int getBaseWorkingDirectoryParentCount( final Path baseDirectory, final Path workingDirectory )
{
return Math.max( 0, workingDirectory.normalize().getNameCount() - baseDirectory.normalize().getNameCount() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void testBranch()
builder.setScmSourceUrl( "scm-url" );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( "pom.xml" );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );

Expand Down Expand Up @@ -133,6 +134,7 @@ public void testCommitMultiModuleDeepFolders()
builder.setScmSourceUrl( scmUrl );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( "pom.xml" );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );
builder.setScmBranchBase( "http://svn.example.com/repos/project/branches/" );
Expand Down Expand Up @@ -173,7 +175,8 @@ public void testCommitForFlatMultiModule()
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
builder.setScmSourceUrl( rootProject.getScm().getConnection() );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setWorkingDirectory( getWorkingDirectory( "rewrite-for-release/pom-with-parent-flat" ).toRealPath( LinkOption.NOFOLLOW_LINKS ).toString() );
builder.setPomFileName( "root-project/pom.xml" );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );

Expand Down Expand Up @@ -215,6 +218,7 @@ public void testCommitMultiModule()
builder.setScmSourceUrl( "scm-url" );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( "pom.xml" );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );

Expand Down Expand Up @@ -267,6 +271,7 @@ public void testSimulateBranch()
builder.setScmSourceUrl( "scm-url" );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( "pom.xml" );
builder.setScmReleaseLabel( "release-label" );

ScmProvider scmProviderMock = mock( ScmProvider.class );
Expand Down Expand Up @@ -431,6 +436,7 @@ private static ReleaseDescriptorBuilder createReleaseDescriptorBuilder()
}

builder.setWorkingDirectory( getPath( workingDir ) );
builder.setPomFileName( "pom.xml" );
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -84,6 +85,13 @@ public static String getPath( File file )
{
return file.toPath().toRealPath( LinkOption.NOFOLLOW_LINKS ).toString();
}

public static String getPath( String file )
throws IOException
{
return Paths.get( file ).toRealPath( LinkOption.NOFOLLOW_LINKS ).toString();
}


@Test
public void testTag()
Expand All @@ -95,6 +103,7 @@ public void testTag()
builder.setScmSourceUrl( "scm-url" );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( rootProject.getFile().getName() );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );

Expand Down Expand Up @@ -132,6 +141,7 @@ public void testCommitMultiModuleDeepFolders()
builder.setScmSourceUrl( scmUrl );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( rootProject.getFile().getName() );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );
builder.setScmTagBase( "http://svn.example.com/repos/project/releases/" );
Expand Down Expand Up @@ -172,7 +182,8 @@ public void testCommitForFlatMultiModule()
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
builder.setScmSourceUrl( rootProject.getScm().getConnection() );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setWorkingDirectory( getWorkingDirectory( "rewrite-for-release/pom-with-parent-flat" ).toRealPath( LinkOption.NOFOLLOW_LINKS ).toString() );
builder.setPomFileName( "root-project/pom.xml" );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );

Expand Down Expand Up @@ -214,6 +225,7 @@ public void testCommitMultiModule()
builder.setScmSourceUrl( "scm-url" );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( rootProject.getFile().getName() );
builder.setScmReleaseLabel( "release-label" );
builder.setScmCommentPrefix( "[my prefix] " );

Expand Down Expand Up @@ -266,6 +278,7 @@ public void testSimulateTag()
builder.setScmSourceUrl( "scm-url" );
MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
builder.setWorkingDirectory( getPath( rootProject.getFile().getParentFile() ) );
builder.setPomFileName( rootProject.getFile().getName() );
builder.setScmReleaseLabel( "release-label" );

ScmProvider scmProviderMock = mock( ScmProvider.class );
Expand Down Expand Up @@ -426,6 +439,7 @@ private static ReleaseDescriptorBuilder createReleaseDescriptorBuilder()
workingDir.mkdirs() );
}
builder.setWorkingDirectory( getPath( workingDir ) );
builder.setPomFileName( "pom.xml" );
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.Os;
Expand All @@ -49,137 +47,6 @@ public void testProjectIsNull()
assertNull( ReleaseUtil.getStandardPom( null ) );
}

@Test
public void testGetCommonBasedirSingleProject() throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module/project" ),
ReleaseUtil.getCommonBasedir( Collections.singletonList( createProject( "/working/directory/flat-multi-module/project" ) ) ) );
}

@Test
public void testGetCommonBasedirSingleProjectWindows() throws Exception
{
assumeTrue( Os.isFamily( Os.FAMILY_WINDOWS ) );

assertEquals( Paths.get( "C:\\working\\directory\\flat-multi-module\\project" ),
ReleaseUtil.getCommonBasedir( Collections.singletonList( createProject( "C:\\working\\directory\\flat-multi-module\\project" ) ) ) );
}

@Test
public void testGetCommonBasedirOfFlatMultiModule()
throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "/working/directory/flat-multi-module/root-project" ),
createProject( "/working/directory/flat-multi-module/core" ),
createProject( "/working/directory/flat-multi-module/webapp" ) ) ) );
}

@Test
public void testGetCommonBasedirOfFlatMultiModuleWindows()
throws Exception
{
assumeTrue( Os.isFamily( Os.FAMILY_WINDOWS ) );

assertEquals( Paths.get( "C:\\working\\directory\\flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "C:\\working\\directory\\flat-multi-module\\root-project" ),
createProject( "C:\\working\\directory\\flat-multi-module\\core" ),
createProject( "C:\\working\\directory\\flat-multi-module\\webapp" ) ) ) );
}

@Test
public void testGetCommonBasedirUppercaseLowerCaseWindows()
throws Exception
{
assumeTrue( Os.isFamily( Os.FAMILY_WINDOWS ) );

assertEquals( Paths.get( "C:\\WORKING\\root" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "c:\\WORKING\\root",
"C:\\WORKING\\root" ),
createProject( "c:\\working\\root\\project1",
"C:\\WORKING\\root\\project1" ),
createProject( "C:\\WORKING\\root\\project2",
"C:\\WORKING\\root\\project2" ) ) ) );
}

@Test
public void testGetCommonBasedirOfFlatMultiModuleSimilarArtifactIds()
throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "/working/directory/flat-multi-module/release-parent" ),
createProject( "/working/directory/flat-multi-module/release-module1" ),
createProject( "/working/directory/flat-multi-module/release-module2" ) ) ) );
}

@Test
public void testGetCommonBasedirOfFlatMultiModuleSimilarArtifactIdsWindows()
throws Exception
{
assumeTrue( Os.isFamily( Os.FAMILY_WINDOWS ) );

assertEquals( Paths.get( "c:\\working\\directory\\flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "c:\\working\\directory\\flat-multi-module\\release-parent" ),
createProject( "c:\\working\\directory\\flat-multi-module\\release-module1" ),
createProject( "c:\\working\\directory\\flat-multi-module\\release-module2" ) ) ) );
}

@Test
public void testGetCommonBasedirOfRegularMultiModule()
throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "/working/directory/flat-multi-module" ),
createProject( "/working/directory/flat-multi-module/core" ),
createProject( "/working/directory/flat-multi-module/webapp" ) ) ) );
}

@Test
public void testGetCommonBasedirOfRegularMultiModuleParentNotBeeingFirstInReactor()
throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "/working/directory/flat-multi-module/core" ),
createProject( "/working/directory/flat-multi-module" ),
createProject( "/working/directory/flat-multi-module/webapp" ) ) ) );
}

@Test
public void testGetCommonBasedirOfRegularMultiModuleWindowsPath()
throws Exception
{
assumeTrue( Os.isFamily( Os.FAMILY_WINDOWS ) );

assertEquals( Paths.get( "c:\\working\\directory\\flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "c:\\working\\directory\\flat-multi-module\\core" ),
createProject( "c:\\working\\directory\\flat-multi-module" ),
createProject( "c:\\working\\directory\\flat-multi-module\\webapp" ) ) ) );
}

@Test
public void testGetCommonBasedirOfFlatMultiModuleWithMultipleLevels()
throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "/working/directory/flat-multi-module/root-project" ),
createProject( "/working/directory/flat-multi-module/core" ),
createProject( "/working/directory/flat-multi-module/common/utils" ),
createProject( "/working/directory/flat-multi-module/common/xml" ),
createProject( "/working/directory/flat-multi-module/webapp" ) ) ) );
}

@Test
public void testGetCommonBasedirOfFlatMultiModuleWithDescendingHierarchy()
throws Exception
{
assertEquals( Paths.get( "/working/directory/flat-multi-module" ),
ReleaseUtil.getCommonBasedir( Arrays.asList( createProject( "/working/directory/flat-multi-module/level/1/2/3" ),
createProject( "/working/directory/flat-multi-module/level/1/2" ),
createProject( "/working/directory/flat-multi-module/level/1" ),
createProject( "/working/directory/flat-multi-module/level" ),
createProject( "/working/directory/flat-multi-module/other" ) ) ) );
}

@Test
public void testGetBaseScmUrlSingleLevel()
throws Exception
Expand Down Expand Up @@ -359,7 +226,7 @@ public void testGetWindowsRootBaseWorkingDirectoryParentCountDifferentCase()

private static MavenProject createProject( String basedir )
{
return createProject( basedir, basedir );
return createProject( basedir, basedir );
}

private static MavenProject createProject( final String basedirPath, final String basedirCanonicalPath )
Expand All @@ -371,11 +238,11 @@ public File getBasedir()
{
return new File( basedirPath )
{
@Override
@Override
public String getCanonicalPath()
{
return basedirCanonicalPath;
}
{
return basedirCanonicalPath;
}
};
}
};
Expand Down
Loading

0 comments on commit eabcfb8

Please sign in to comment.