Skip to content

Commit

Permalink
[MSHARED-649] Allow Pom file outside base directory
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jan 27, 2022
1 parent 2c85696 commit ca84e5c
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 125 deletions.
16 changes: 6 additions & 10 deletions src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,33 @@ public InvocationResult execute( InvocationRequest request )
{
MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder();

InvokerLogger logger = getLogger();
if ( logger != null )
{
cliBuilder.setLogger( getLogger() );
cliBuilder.setLogger( logger );
}

File localRepo = getLocalRepositoryDirectory();
if ( localRepo != null )
if ( localRepositoryDirectory != null )
{
cliBuilder.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
cliBuilder.setLocalRepositoryDirectory( localRepositoryDirectory );
}

File mavenHome = getMavenHome();
if ( mavenHome != null )
{
cliBuilder.setMavenHome( getMavenHome() );
cliBuilder.setMavenHome( mavenHome );
}

File mavenExecutable = getMavenExecutable();
if ( mavenExecutable != null )
{
cliBuilder.setMavenExecutable( mavenExecutable );
}

File workingDirectory = getWorkingDirectory();
if ( workingDirectory != null )
{
cliBuilder.setWorkingDirectory( getWorkingDirectory() );
cliBuilder.setBaseDirectory( workingDirectory );
}

Commandline cli;

try
{
cli = cliBuilder.build( request );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MavenCommandLineBuilder

private InvokerLogger logger = DEFAULT_LOGGER;

private File workingDirectory;
private File baseDirectory;

private File localRepositoryDirectory;

Expand Down Expand Up @@ -89,10 +89,15 @@ public Commandline build( InvocationRequest request )
// includes/excludes, etc.
setReactorBehavior( request, cli );

// working directory and local repository location
setEnvironmentPaths( request, cli );
// discover value for working directory
setupBaseDirectory( request );

// pom-file and basedir handling
cli.setWorkingDirectory( baseDirectory );

// local repository location
setLocalRepository( request, cli );

// pom-file handling
setPomLocation( request, cli );

setSettingsLocation( request, cli );
Expand Down Expand Up @@ -296,52 +301,33 @@ protected void setProperties( InvocationRequest request, Commandline cli )
*/
protected void setPomLocation( InvocationRequest request, Commandline cli )
{
boolean pomSpecified = false;

File pom = request.getPomFile();
String pomFilename = request.getPomFileName();
File baseDirectory = request.getBaseDirectory();

if ( pom != null )
if ( pom == null )
{
pomSpecified = true;
}
else if ( baseDirectory != null )
{
if ( baseDirectory.isDirectory() )
if ( pomFilename != null )
{
if ( pomFilename != null )
{
pom = new File( baseDirectory, pomFilename );

pomSpecified = true;
}
else
{
pom = new File( baseDirectory, "pom.xml" );
}
pom = new File( baseDirectory, pomFilename );
}
else
{
logger.warn( "Base directory is a file. Using base directory as POM location." );

pom = baseDirectory;

pomSpecified = true;
pom = new File( baseDirectory, "pom.xml" );
}
}

if ( pomSpecified )
try
{
try
{
pom = pom.getCanonicalFile();
}
catch ( IOException e )
{
logger.debug( "Failed to canonicalize the POM path: " + pom + ". Using as-is.", e );
}
pom = pom.getCanonicalFile();
}
catch ( IOException e )
{
logger.debug( "Failed to canonicalize the POM path: " + pom + ". Using as-is.", e );
}

if ( pom.getParentFile().equals( baseDirectory ) )
{
// pom in project workspace
if ( !"pom.xml".equals( pom.getName() ) )
{
logger.debug( "Specified POM file is not named 'pom.xml'. "
Expand All @@ -351,55 +337,64 @@ else if ( baseDirectory != null )
cli.createArg().setValue( pom.getName() );
}
}
else
{
cli.createArg().setValue( "-f" );
cli.createArg().setValue( pom.getPath() );
}
}

/**
* <p>setEnvironmentPaths.</p>
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setEnvironmentPaths( InvocationRequest request, Commandline cli )
void setupBaseDirectory( InvocationRequest request )
{
File workingDirectory = request.getBaseDirectory();

if ( workingDirectory == null )
File baseDirectoryFromRequest = null;
if ( request.getBaseDirectory() != null )
{
baseDirectoryFromRequest = request.getBaseDirectory();
}
else
{
File pomFile = request.getPomFile();
if ( pomFile != null )
{
workingDirectory = pomFile.getParentFile();
baseDirectoryFromRequest = pomFile.getParentFile();
}
}

if ( workingDirectory == null )
if ( baseDirectoryFromRequest != null )
{
workingDirectory = this.workingDirectory;
baseDirectory = baseDirectoryFromRequest;
}

if ( workingDirectory == null )
if ( baseDirectory == null )
{
workingDirectory = new File( System.getProperty( "user.dir" ) );
baseDirectory = new File( System.getProperty( "user.dir" ) );
}
else if ( workingDirectory.isFile() )
else if ( baseDirectory.isFile() )
{
logger.warn( "Specified base directory (" + workingDirectory + ") is a file."
logger.warn( "Specified base directory (" + baseDirectory + ") is a file."
+ " Using its parent directory..." );

workingDirectory = workingDirectory.getParentFile();
baseDirectory = baseDirectory.getParentFile();
}

try
{
cli.setWorkingDirectory( workingDirectory.getCanonicalPath() );
baseDirectory = baseDirectory.getCanonicalFile();
}
catch ( IOException e )
{
logger.debug( "Failed to canonicalize base directory: " + workingDirectory + ". Using as-is.", e );

cli.setWorkingDirectory( workingDirectory.getAbsolutePath() );
logger.debug( "Failed to canonicalize base directory: " + baseDirectory + ". Using as-is.", e );
}
}

/**
* <p>setLocalRepository.</p>
*
* @param request a {@link org.apache.maven.shared.invoker.InvocationRequest} object.
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setLocalRepository( InvocationRequest request, Commandline cli )
{
File localRepositoryDirectory = request.getLocalRepositoryDirectory( this.localRepositoryDirectory );

if ( localRepositoryDirectory != null )
Expand Down Expand Up @@ -708,23 +703,23 @@ public void setMavenHome( File mavenHome )
}

/**
* <p>Getter for the field <code>workingDirectory</code>.</p>
* <p>Getter for the field <code>baseDirectory</code>.</p>
*
* @return a {@link java.io.File} object.
*/
public File getWorkingDirectory()
public File getBaseDirectory()
{
return workingDirectory;
return baseDirectory;
}

/**
* <p>Setter for the field <code>workingDirectory</code>.</p>
* <p>Setter for the field <code>baseDirectory</code>.</p>
*
* @param workingDirectory a {@link java.io.File} object.
* @param baseDirectory a {@link java.io.File} object.
*/
public void setWorkingDirectory( File workingDirectory )
public void setBaseDirectory( File baseDirectory )
{
this.workingDirectory = workingDirectory;
this.baseDirectory = baseDirectory;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ public void testSpaceProperties()
assertEquals( 0, result.getExitCode() );
}

@Test
public void testPomOutsideProject() throws Exception
{
logTestStart();

File testDir = getBasedirForBuild();

File basedir = new File( testDir, "project" );
File pom = new File(testDir, "temp/pom.xml" );

Invoker invoker = newInvoker();

InvocationRequest request = new DefaultInvocationRequest();
request.setBaseDirectory( basedir );
request.setPomFile( pom );
request.setGoals( Collections.singletonList( "validate" ) );

InvocationResult result = invoker.execute( request );

assertEquals( 0, result.getExitCode() );
}

private Invoker newInvoker()
{
Invoker invoker = new DefaultInvoker();
Expand Down
Loading

0 comments on commit ca84e5c

Please sign in to comment.