Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public class MavenProject

public static final String EMPTY_PROJECT_VERSION = "0";

private static final MavenProject ERROR_BUILDING_PARENT = new MavenProject();

private Model model;

private MavenProject parent;
Expand Down Expand Up @@ -344,6 +346,10 @@ public Model getModel()
return model;
}

/**
* Returns the project corresponding to a declared parent.
* @return the parent, or null if no parent is declared or there was an error building it
*/
public MavenProject getParent()
{
if ( parent == null )
Expand All @@ -364,7 +370,11 @@ public MavenProject getParent()
}
catch ( ProjectBuildingException e )
{
throw new IllegalStateException( "Failed to build parent project for " + getId(), e );
if ( logger != null )
{
logger.error( "Failed to build parent project for " + getId(), e );
}
parent = ERROR_BUILDING_PARENT;
}
}
else if ( model.getParent() != null )
Expand All @@ -379,11 +389,15 @@ else if ( model.getParent() != null )
}
catch ( ProjectBuildingException e )
{
throw new IllegalStateException( "Failed to build parent project for " + getId(), e );
if ( logger != null )
{
logger.error( "Failed to build parent project for " + getId(), e );
}
parent = ERROR_BUILDING_PARENT;
}
}
}
return parent;
return parent == ERROR_BUILDING_PARENT ? null : parent;
}

public void setParent( MavenProject parent )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.maven.lifecycle.internal.stub.LoggerStub;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
Expand Down Expand Up @@ -177,6 +179,32 @@ public void testCloneWithActiveProfile()
activeProfilesClone );
}

public void testInvalidParent() throws Exception
{
Parent parent = new Parent();
parent.setGroupId( "test-group" );
parent.setArtifactId( "parent-artifact" );
parent.setVersion( "1.0" );
Model model = new Model();
model.setParent( parent );
model.setArtifactId( "child-artifact" );
final AtomicInteger logged = new AtomicInteger();
class L extends LoggerStub
{
@Override
public void error( String s, Throwable throwable )
{
logged.incrementAndGet();
}
}
MavenProject project = new MavenProject( repositorySystem, projectBuilder, newBuildingRequest(), new L() );
project.setModel( model );
assertNull( project.getParent() );
assertEquals( 1, logged.get() );
assertNull( project.getParent() );
assertEquals( 1, logged.get() );
}

public void testUndefinedOutputDirectory()
throws Exception
{
Expand Down