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 68f1b3194c..ce4c33fa8c 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -16,6 +16,14 @@ package org.apache.maven; * limitations under the License. */ +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + import org.apache.maven.lifecycle.goal.GoalNotFoundException; import org.apache.maven.lifecycle.session.MavenSession; import org.apache.maven.lifecycle.session.MavenSessionPhaseManager; @@ -24,6 +32,7 @@ import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; + import org.codehaus.plexus.ArtifactEnabledContainer; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.context.Context; @@ -34,23 +43,15 @@ import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - public class DefaultMaven extends AbstractLogEnabled implements Maven, Contextualizable { private ArtifactEnabledContainer container; - private String mavenHome; + private File mavenHome; - private String mavenHomeLocal; + private File mavenHomeLocal; private boolean logResults = true; @@ -183,9 +184,7 @@ public class DefaultMaven Runtime r = Runtime.getRuntime(); - getLogger().info( - "Final Memory: " + ((r.totalMemory() - r.freeMemory()) / mb) + "M/" + (r.totalMemory() / mb) + "M" ); - + getLogger().info( "Final Memory: " + ((r.totalMemory() - r.freeMemory()) / mb) + "M/" + (r.totalMemory() / mb) + "M" ); } private void line() @@ -197,8 +196,8 @@ public class DefaultMaven // Reactor execution // ---------------------------------------------------------------------- - public ExecutionResponse executeReactor( String goals, String includes, String excludes ) throws ReactorException, - GoalNotFoundException + public ExecutionResponse executeReactor( String goals, String includes, String excludes ) + throws ReactorException, GoalNotFoundException { List projects = new ArrayList(); @@ -210,9 +209,9 @@ public class DefaultMaven for ( Iterator iterator = files.iterator(); iterator.hasNext(); ) { - File f = (File) iterator.next(); + File file = (File) iterator.next(); - MavenProject project = projectBuilder.build( f ); + MavenProject project = projectBuilder.build( getMavenHomeLocal(), file ); projects.add( project ); } @@ -278,7 +277,8 @@ public class DefaultMaven // Project building // ---------------------------------------------------------------------- - public MavenProject getProject( File project ) throws ProjectBuildingException + public MavenProject getProject( File project ) + throws ProjectBuildingException { if ( project.exists() ) { @@ -288,7 +288,7 @@ public class DefaultMaven } } - return projectBuilder.build( project ); + return projectBuilder.build( getMavenHomeLocal(), project ); } // ---------------------------------------------------------------------- @@ -301,26 +301,36 @@ public class DefaultMaven } // ---------------------------------------------------------------------- - // Maven home + // Maven Configuration // ---------------------------------------------------------------------- - public void setMavenHome( String mavenHome ) + public void setMavenHome( File mavenHome ) { this.mavenHome = mavenHome; } - public String getMavenHome() + public File getMavenHome() { + if ( mavenHomeLocal == null ) + { + throw new NullPointerException( "Maven home must be set." ); + } + return mavenHome; } - public void setMavenHomeLocal( String mavenHomeLocal ) + public void setMavenHomeLocal( File mavenHomeLocal ) { this.mavenHomeLocal = mavenHomeLocal; } - public String getMavenHomeLocal() + public File getMavenHomeLocal() { + if ( mavenHomeLocal == null ) + { + throw new NullPointerException( "Maven home local must be set." ); + } + return mavenHomeLocal; } diff --git a/maven-core/src/main/java/org/apache/maven/Maven.java b/maven-core/src/main/java/org/apache/maven/Maven.java index 5794c6e7d5..407f04ab93 100644 --- a/maven-core/src/main/java/org/apache/maven/Maven.java +++ b/maven-core/src/main/java/org/apache/maven/Maven.java @@ -67,13 +67,13 @@ public interface Maven // Maven home // ---------------------------------------------------------------------- - void setMavenHome( String mavenHome ); + void setMavenHome( File mavenHome ); - String getMavenHome(); + File getMavenHome(); - void setMavenHomeLocal( String mavenHomeLocal ); + void setMavenHomeLocal( File mavenHomeLocal ); - String getMavenHomeLocal(); + File getMavenHomeLocal(); // ---------------------------------------------------------------------- // Maven project handling diff --git a/maven-core/src/main/java/org/apache/maven/MavenCli.java b/maven-core/src/main/java/org/apache/maven/MavenCli.java index fed8d7b697..6351f8133f 100644 --- a/maven-core/src/main/java/org/apache/maven/MavenCli.java +++ b/maven-core/src/main/java/org/apache/maven/MavenCli.java @@ -16,6 +16,10 @@ package org.apache.maven; * limitations under the License. */ +import java.io.File; +import java.util.Iterator; +import java.util.TreeMap; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; @@ -24,16 +28,9 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; import org.apache.maven.plugin.descriptor.MojoDescriptor; + import org.codehaus.classworlds.ClassWorld; import org.codehaus.plexus.embed.ArtifactEnabledEmbedder; -import org.codehaus.plexus.embed.Embedder; -import org.codehaus.plexus.util.StringUtils; - -import java.io.File; -import java.io.FileInputStream; -import java.util.Iterator; -import java.util.Properties; -import java.util.TreeMap; /** * @author Jason van Zyl @@ -62,9 +59,9 @@ public class MavenCli Maven maven = (Maven) embedder.lookup( Maven.ROLE ); - maven.setMavenHome( System.getProperty( "maven.home" ) ); + maven.setMavenHome( new File( System.getProperty( "maven.home" ) ) ); - maven.setMavenHomeLocal( System.getProperty( "maven.home.local", System.getProperty( "user.home" ) + "/.m2" ) ); + maven.setMavenHomeLocal( new File( System.getProperty( "maven.home.local", System.getProperty( "user.home" ) + "/.m2" ) ) ); //--- @@ -75,6 +72,14 @@ public class MavenCli if ( !projectFile.exists() ) { projectFile = new File( System.getProperty( "user.dir" ), POMv3 ); + + if ( !projectFile.exists() ) + { + System.err.println( "Could not find either a " + POMv4 + " nor a " + POMv3 + " project descriptor." ); + + // TODO: Use some constant for this value. Trygve. + return 1; + } } // ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalMappingPhase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalMappingPhase.java index b3e625e1d5..67b04ce347 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalMappingPhase.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalMappingPhase.java @@ -1,21 +1,33 @@ -/* Created on Sep 21, 2004 */ package org.apache.maven.lifecycle.goal.phase; +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 java.util.HashSet; -import java.util.Iterator; -import java.util.List; import java.util.Set; import org.apache.maven.lifecycle.goal.AbstractMavenGoalPhase; import org.apache.maven.lifecycle.goal.GoalExecutionException; import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; -import org.apache.maven.lifecycle.goal.phase.PluginResolutionPhase.PluginResolutionVisitor; import org.apache.maven.lifecycle.session.MavenSession; import org.apache.maven.plugin.PluginManager; -import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.util.AbstractGoalVisitor; import org.apache.maven.util.GoalWalker; import org.apache.maven.util.GraphTraversalException; + import org.codehaus.plexus.util.dag.CycleDetectedException; /** diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalResolutionPhase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalResolutionPhase.java index 03101df076..b3f514b257 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalResolutionPhase.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/GoalResolutionPhase.java @@ -16,29 +16,20 @@ package org.apache.maven.lifecycle.goal.phase; * limitations under the License. */ -import org.apache.maven.lifecycle.goal.AbstractMavenGoalPhase; -import org.apache.maven.lifecycle.goal.GoalExecutionException; -import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; -import org.apache.maven.lifecycle.goal.phase.PluginResolutionPhase.PluginResolutionVisitor; -import org.apache.maven.lifecycle.session.MavenSession; -import org.apache.maven.model.GoalDecorator; -import org.apache.maven.model.PreGoal; -import org.apache.maven.plugin.PluginManager; -import org.apache.maven.plugin.descriptor.MojoDescriptor; -import org.apache.maven.util.AbstractGoalVisitor; -import org.apache.maven.util.GoalWalker; -import org.apache.maven.util.GraphTraversalException; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.TreeMap; +import org.apache.maven.lifecycle.goal.AbstractMavenGoalPhase; +import org.apache.maven.lifecycle.goal.GoalExecutionException; +import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; +import org.apache.maven.lifecycle.session.MavenSession; +import org.apache.maven.plugin.PluginManager; +import org.apache.maven.util.AbstractGoalVisitor; +import org.apache.maven.util.GoalWalker; +import org.apache.maven.util.GraphTraversalException; + /** * @author Jason van Zyl * @version $Id$ diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/PluginResolutionPhase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/PluginResolutionPhase.java index f66e30cc17..1f8396a2a0 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/PluginResolutionPhase.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/goal/phase/PluginResolutionPhase.java @@ -1,25 +1,32 @@ -/* Created on Sep 21, 2004 */ package org.apache.maven.lifecycle.goal.phase; +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.TreeMap; import org.apache.maven.lifecycle.goal.AbstractMavenGoalPhase; import org.apache.maven.lifecycle.goal.GoalExecutionException; import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; import org.apache.maven.lifecycle.session.MavenSession; import org.apache.maven.plugin.PluginManager; -import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.util.AbstractGoalVisitor; import org.apache.maven.util.GoalWalker; import org.apache.maven.util.GraphTraversalException; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.util.dag.CycleDetectedException; /** * @author jdcasey diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java index 18fa0292b3..b880ad1b8a 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginManager.java @@ -16,12 +16,11 @@ package org.apache.maven.plugin; * limitations under the License. */ +import java.util.Map; + import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.plugin.descriptor.MojoDescriptor; -import java.util.List; -import java.util.Map; - /** * @author Jason van Zyl * @version $Id$ diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 9bbcaee309..02d37a18f5 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -16,6 +16,21 @@ package org.apache.maven.project; * limitations under the License. */ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.MavenMetadataSource; @@ -34,27 +49,14 @@ import org.apache.maven.project.path.PathTranslator; import org.apache.maven.project.validation.ModelValidationResult; import org.apache.maven.project.validation.ModelValidator; import org.apache.maven.repository.RepositoryUtils; + import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.dag.DAG; import org.codehaus.plexus.util.dag.TopologicalSorter; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - public class DefaultMavenProjectBuilder extends AbstractLogEnabled implements MavenProjectBuilder, Initializable @@ -73,8 +75,6 @@ public class DefaultMavenProjectBuilder private PathTranslator pathTranslator; - private Model superModel; - public void initialize() throws Exception { @@ -83,10 +83,14 @@ public class DefaultMavenProjectBuilder modelReader = new MavenXpp3Reader(); } - public MavenProject build( File projectDescriptor ) + // ---------------------------------------------------------------------- + // MavenProjectBuilder Implementation + // ---------------------------------------------------------------------- + + public MavenProject build( File mavenLocalHome, File projectDescriptor ) throws ProjectBuildingException { - return build( projectDescriptor, false ); + return build( mavenLocalHome, projectDescriptor, false ); } /** @todo can we move the super model reading to the initialize method? what about the user/site? Is it reused? @@ -94,51 +98,14 @@ public class DefaultMavenProjectBuilder * @todo we should be passing in some more configuration here so that maven home local can be used for user properties. Then, the new stuff should be unit tested. * @todo the user model bit overwriting the super model seems a bit gross, but is needed so that any repositories given take affect */ - public MavenProject build( File projectDescriptor, boolean resolveDependencies ) + public MavenProject build( File mavenLocalHome, File projectDescriptor, boolean resolveDependencies ) throws ProjectBuildingException { - String localRepositoryValue = null; + ArtifactRepository localRepository = getLocalRepository( mavenLocalHome ); try { - // TODO: rename to super-pom.xml so it is not used by the reactor - superModel = modelReader.read( new InputStreamReader( DefaultMavenProjectBuilder.class.getResourceAsStream( "pom-4.0.0.xml" ) ) ); - - Model userModel = null; - // TODO: use maven home local instead of user.home/.m2 - File userModelFile = new File( System.getProperty( "user.home" ) + "/.m2", "override.xml" ); - if ( userModelFile.exists() ) - { - userModel = modelReader.read( new FileReader( userModelFile ) ); - if ( userModel.getParent() != null ) - { - throw new ProjectBuildingException( "Inheritence not supported in the user override POM" ); - } - - if ( userModel.getLocal() != null && userModel.getLocal().getRepository() != null ) - { - localRepositoryValue = userModel.getLocal().getRepository(); - } - superModel.getRepositories().addAll( userModel.getRepositories() ); - } - - if ( localRepositoryValue == null && superModel.getLocal() != null && superModel.getLocal().getRepository() != null ) - { - localRepositoryValue = superModel.getLocal().getRepository(); - } - - localRepositoryValue = System.getProperty( "maven.repo.local", localRepositoryValue ); - System.setProperty( "maven.repo.local", localRepositoryValue ); - - ArtifactRepository localRepository = null; - if ( localRepositoryValue != null ) - { - localRepository = RepositoryUtils.localRepositoryToWagonRepository( localRepositoryValue ); - } - else - { - throw new ProjectBuildingException( "A local repository must be specified" ); - } + Model superModel = getSuperModel(); LinkedList lineage = new LinkedList(); @@ -156,6 +123,8 @@ public class DefaultMavenProjectBuilder previous = current; } + Model userModel = getUserOverrideModel( superModel, mavenLocalHome ); + if ( userModel != null ) { modelInheritanceAssembler.assembleModelInheritance( userModel, previous ); @@ -236,15 +205,15 @@ public class DefaultMavenProjectBuilder if ( parentModel != null ) { - if ( isEmpty( parentModel.getGroupId() ) ) + if ( StringUtils.isEmpty( parentModel.getGroupId() ) ) { throw new ProjectBuildingException( "Missing groupId element from parent element" ); } - else if ( isEmpty( parentModel.getArtifactId() ) ) + else if ( StringUtils.isEmpty( parentModel.getArtifactId() ) ) { throw new ProjectBuildingException( "Missing artifactId element from parent element" ); } - else if ( isEmpty( parentModel.getVersion() ) ) + else if ( StringUtils.isEmpty( parentModel.getVersion() ) ) { throw new ProjectBuildingException( "Missing version element from parent element" ); } @@ -281,33 +250,32 @@ public class DefaultMavenProjectBuilder } private Model readModel( File projectDescriptor ) - throws Exception + throws ProjectBuildingException { - Reader reader = null; - try { - reader = new FileReader( projectDescriptor ); + return readModel( new FileReader( projectDescriptor ) ); + } + catch( FileNotFoundException ex ) + { + throw new ProjectBuildingException( "Error while building model.", ex ); + } + } - Model model = modelReader.read( reader ); - - reader.close(); - - return model; + private Model readModel( Reader reader ) + throws ProjectBuildingException + { + try + { + return modelReader.read( reader ); + } + catch( Exception ex ) + { + throw new ProjectBuildingException( "Error while building model.", ex ); } finally { - if ( reader != null ) - { - try - { - reader.close(); - } - catch ( IOException e ) - { - // ignore - } - } + IOUtil.close( reader ); } } @@ -411,8 +379,82 @@ public class DefaultMavenProjectBuilder return sortedProjects; } - private boolean isEmpty( String string ) + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + /** + * Locate the local repository. + * + *
    + *
  1. Try ${maven.repo.local} + *
  2. Look in mavenHomeLocal/override.xml + *
  3. Set to the default value (${user.home}/.m2/repository). + *
+ * + * @param mavenHomeLocal The maven local home directory + * @return Returns the local repository + * @throws ProjectBuildingException + */ + protected ArtifactRepository getLocalRepository( File mavenHomeLocal ) + throws ProjectBuildingException { - return string == null || string.trim().length() == 0; + String localRepository = System.getProperty( "maven.repo.local" ); + + Model superModel = getSuperModel(); + + if ( !StringUtils.isEmpty( localRepository ) ) + { + return RepositoryUtils.localRepositoryToWagonRepository( localRepository ); + } + + if ( superModel.getLocal() != null && superModel.getLocal().getRepository() != null ) + { + localRepository = superModel.getLocal().getRepository(); + } + + Model userModel = getUserOverrideModel( superModel, mavenHomeLocal ); + + if ( userModel != null && userModel.getLocal() != null && userModel.getLocal().getRepository() != null ) + { + localRepository = userModel.getLocal().getRepository(); + } + + if ( localRepository == null ) + { + String userHome = System.getProperty( "user.home" ); + + localRepository = new File( userHome, ".m2/repository" ).getAbsolutePath(); + } + + return RepositoryUtils.localRepositoryToWagonRepository( localRepository ); + } + + private Model getSuperModel() + throws ProjectBuildingException + { + return readModel( new InputStreamReader( DefaultMavenProjectBuilder.class.getResourceAsStream( "pom-4.0.0.xml" ) ) ); + } + + private Model getUserOverrideModel( Model superModel, File mavenHomeLocal ) + throws ProjectBuildingException + { + File userModelFile = new File( mavenHomeLocal, "override.xml" ); + + if ( !userModelFile.exists() ) + { + return null; + } + + Model model = readModel( userModelFile ); + + if ( model.getParent() != null ) + { + throw new ProjectBuildingException( "Inheritence not supported in the user override POM" ); + } + + superModel.getRepositories().addAll( model.getRepositories() ); + + return model; } } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java index 76dc955373..457d4d19e1 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java @@ -25,10 +25,10 @@ public interface MavenProjectBuilder // ---------------------------------------------------------------------- - MavenProject build( File project ) + MavenProject build( File mavenHomeLocal, File project ) throws ProjectBuildingException; - MavenProject build( File project, boolean followTransitiveDeps ) + MavenProject build( File mavenHomeLocal, File project, boolean followTransitiveDeps ) throws ProjectBuildingException; // ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/util/AbstractGoalVisitor.java b/maven-core/src/main/java/org/apache/maven/util/AbstractGoalVisitor.java index 27b82cfbc4..1212ad77bd 100644 --- a/maven-core/src/main/java/org/apache/maven/util/AbstractGoalVisitor.java +++ b/maven-core/src/main/java/org/apache/maven/util/AbstractGoalVisitor.java @@ -1,8 +1,22 @@ -/* Created on Sep 21, 2004 */ package org.apache.maven.util; +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.lifecycle.session.MavenSession; -import org.apache.maven.plugin.descriptor.MojoDescriptor; /** * @author jdcasey diff --git a/maven-core/src/main/java/org/apache/maven/util/GoalVisitor.java b/maven-core/src/main/java/org/apache/maven/util/GoalVisitor.java index 6b78439059..79752630b3 100644 --- a/maven-core/src/main/java/org/apache/maven/util/GoalVisitor.java +++ b/maven-core/src/main/java/org/apache/maven/util/GoalVisitor.java @@ -1,8 +1,22 @@ -/* Created on Sep 21, 2004 */ package org.apache.maven.util; +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.lifecycle.session.MavenSession; -import org.apache.maven.plugin.descriptor.MojoDescriptor; /** * @author jdcasey diff --git a/maven-core/src/test/java/org/apache/maven/MavenTestCase.java b/maven-core/src/test/java/org/apache/maven/MavenTestCase.java index eba2d1af51..1900b9fe0e 100644 --- a/maven-core/src/test/java/org/apache/maven/MavenTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/MavenTestCase.java @@ -16,20 +16,18 @@ package org.apache.maven; * limitations under the License. */ +import java.io.File; +import java.util.ArrayList; +import java.util.List; + import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; import org.apache.maven.lifecycle.session.MavenSession; import org.apache.maven.plugin.PluginManager; -import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; -import org.codehaus.classworlds.ClassRealm; -import org.codehaus.classworlds.ClassWorld; -import org.codehaus.plexus.ArtifactEnabledPlexusTestCase; -import java.io.File; -import java.util.ArrayList; -import java.util.List; +import org.codehaus.plexus.ArtifactEnabledPlexusTestCase; /** * @author Jason van Zyl @@ -44,11 +42,17 @@ public class MavenTestCase protected String testRepoUrl; - protected void setUp() throws Exception + private File mavenHome = new File( getBasedir(), "target/maven.home" ); + + private File mavenLocalHome = new File( getBasedir(), "target/maven.home.local" );; + + protected void setUp() + throws Exception { super.setUp(); File testRepoLocation = new File( "target/repo" ); + if ( !testRepoLocation.exists() ) { testRepoLocation.mkdirs(); @@ -62,42 +66,55 @@ public class MavenTestCase projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); } + // ---------------------------------------------------------------------- + // Customizing the PlexusTestCase + // ---------------------------------------------------------------------- + + protected void customizeContext() + throws Exception + { + MavenTestUtils.customizeContext( getContainer(), getTestFile( "" ), mavenHome, mavenLocalHome ); + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + protected String getTestRepoURL() { return testRepoUrl; } - protected void customizeContext() throws Exception + protected File getMavenHome() { - ClassWorld classWorld = new ClassWorld(); - - ClassRealm rootClassRealm = classWorld.newRealm( "root", Thread.currentThread().getContextClassLoader() ); - - getContainer().addContextValue( "rootClassRealm", rootClassRealm ); - - // TODO: are these used? Are they correct? - getContainer().addContextValue( "maven.home", new File( getBasedir(), "target/maven.home" ).getPath() ); - - getContainer().addContextValue( "maven.home.local", - new File( getBasedir(), "target/maven.home.local" ).getPath() ); + return mavenHome; } - protected MavenGoalExecutionContext createGoalExecutionContext() throws Exception + protected File getMavenLocalHome() + { + return mavenLocalHome; + } + + protected MavenGoalExecutionContext createGoalExecutionContext() + throws Exception { return createGoalExecutionContext( null, null ); } - protected MavenGoalExecutionContext createGoalExecutionContext( File pom ) throws Exception + protected MavenGoalExecutionContext createGoalExecutionContext( File pom ) + throws Exception { return createGoalExecutionContext( pom, null ); } - protected MavenGoalExecutionContext createGoalExecutionContext( String goal ) throws Exception + protected MavenGoalExecutionContext createGoalExecutionContext( String goal ) + throws Exception { return createGoalExecutionContext( null, goal ); } - protected MavenGoalExecutionContext createGoalExecutionContext( File pom, String goal ) throws Exception + protected MavenGoalExecutionContext createGoalExecutionContext( File pom, String goal ) + throws Exception { ArtifactRepository localRepository = new ArtifactRepository( "local", testRepoUrl ); @@ -105,13 +122,13 @@ public class MavenTestCase if ( pom != null ) { - project = projectBuilder.build( pom ); + project = projectBuilder.build( mavenLocalHome, pom ); } else { - File f = new File( basedir, "target/test-classes/pom.xml" ); + File f = getTestFile( "target/test-classes/pom.xml" ); - project = projectBuilder.build( f ); + project = projectBuilder.build( mavenLocalHome, f ); } return createGoalExecutionContext( project, localRepository, goal ); diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/goal/phase/GoalAssemblySubProcessTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/goal/phase/GoalAssemblySubProcessTest.java index bc7327999d..bbf780a9b3 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/goal/phase/GoalAssemblySubProcessTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/goal/phase/GoalAssemblySubProcessTest.java @@ -1,14 +1,20 @@ -/* Created on Jul 14, 2004 */ package org.apache.maven.lifecycle.goal.phase; -import org.apache.maven.MavenTestCase; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; -import org.apache.maven.lifecycle.session.MavenSession; -import org.apache.maven.model.Model; -import org.apache.maven.model.PostGoal; -import org.apache.maven.model.PreGoal; -import org.apache.maven.project.MavenProject; +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 java.util.ArrayList; import java.util.Collections; @@ -18,6 +24,14 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; +import org.apache.maven.MavenTestCase; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; +import org.apache.maven.model.Model; +import org.apache.maven.model.PostGoal; +import org.apache.maven.model.PreGoal; +import org.apache.maven.project.MavenProject; + /** The point of this test class is to check out the functioning of the * plugin resolution, goal mapping, and goal resolution phases. These are * intertwined here to make testing easier, but should be separated into their diff --git a/maven-core/src/test/java/org/apache/maven/plugin/OgnlProjectValueExtractorTest.java b/maven-core/src/test/java/org/apache/maven/plugin/OgnlProjectValueExtractorTest.java index 475efe168d..8800f506ea 100644 --- a/maven-core/src/test/java/org/apache/maven/plugin/OgnlProjectValueExtractorTest.java +++ b/maven-core/src/test/java/org/apache/maven/plugin/OgnlProjectValueExtractorTest.java @@ -1,13 +1,13 @@ package org.apache.maven.plugin; +import java.io.File; + import org.apache.maven.MavenTestCase; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.lifecycle.goal.MavenGoalExecutionContext; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; -import java.io.File; - /** * @author Jason van Zyl * @version $Id$ @@ -28,9 +28,9 @@ public class OgnlProjectValueExtractorTest builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); - File f = new File( basedir, "src/test/resources/pom.xml" ); + File f = getTestFile( "src/test/resources/pom.xml" ); - project = builder.build( f ); + project = builder.build( getMavenLocalHome(), f ); context = createGoalExecutionContext(); } @@ -40,7 +40,7 @@ public class OgnlProjectValueExtractorTest { Object value = OgnlProjectValueExtractor.evaluate( "#project.build.directory/classes", context ); - String expected = new File( basedir, "target/test-classes/target/classes" ).getCanonicalPath(); + String expected = getTestFile( "target/test-classes/target/classes" ).getCanonicalPath(); String actual = new File( value.toString() ).getCanonicalPath(); diff --git a/maven-core/src/test/java/org/apache/maven/project/AbstractProjectTestCase.java b/maven-core/src/test/java/org/apache/maven/project/AbstractProjectTestCase.java index 8258fc46c3..302b0542a2 100644 --- a/maven-core/src/test/java/org/apache/maven/project/AbstractProjectTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/AbstractProjectTestCase.java @@ -31,12 +31,12 @@ public class AbstractProjectTestCase return localRepository; } - protected MavenProject buildProject( File f, boolean followTransitiveDeps ) + protected MavenProject buildProject( File file, boolean followTransitiveDeps ) throws Exception { MavenProject project; - project = projectBuilder.build( f, followTransitiveDeps ); + project = projectBuilder.build( getMavenLocalHome(), file, followTransitiveDeps ); assertNotNull( "Project is null", project ); diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBaseDirectoryAlignmentTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBaseDirectoryAlignmentTest.java index df0d447d9d..95c4ce6683 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectBaseDirectoryAlignmentTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBaseDirectoryAlignmentTest.java @@ -14,21 +14,21 @@ public class ProjectBaseDirectoryAlignmentTest public void testProjectDirectoryBaseDirectoryAlignment() throws Exception { - File f = new File( basedir, dir + "project-which-needs-directory-alignment.xml" ); + File f = getTestFile( dir + "project-which-needs-directory-alignment.xml" ); - MavenProject project = projectBuilder.build( f, false ); + MavenProject project = projectBuilder.build( getMavenLocalHome(), f, false ); assertNotNull( "Test project can't be null!", project ); - assertTrue( project.getBuild().getSourceDirectory().startsWith( basedir ) ); + assertTrue( project.getBuild().getSourceDirectory().startsWith( getBasedir() ) ); - assertTrue( project.getBuild().getUnitTestSourceDirectory().startsWith( basedir ) ); + assertTrue( project.getBuild().getUnitTestSourceDirectory().startsWith( getBasedir() ) ); Build build = project.getBuild(); Resource resource = (Resource) build.getResources().get( 0 ); - assertTrue( resource.getDirectory().startsWith( basedir ) ); + assertTrue( resource.getDirectory().startsWith( getBasedir() ) ); } /* TODO: why commented out? Gives a Wagonhttp warning and can't find parent POM diff --git a/maven-core/src/test/java/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.java index 76d3b1bba4..0d59f2f888 100644 --- a/maven-core/src/test/java/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.java @@ -16,26 +16,15 @@ package org.apache.maven.project.canonical; * limitations under the License. */ -import junit.framework.TestCase; -import org.apache.maven.model.Build; -import org.apache.maven.model.Contributor; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.Developer; -import org.apache.maven.model.DistributionManagement; -import org.apache.maven.model.MailingList; -import org.apache.maven.model.Repository; -import org.apache.maven.model.Resource; -import org.apache.maven.model.Site; -import org.apache.maven.model.UnitTest; -import org.apache.maven.model.Plugin; -import org.apache.maven.model.Goal; -import org.apache.maven.project.MavenProject; -import org.apache.maven.MavenTestCase; - import java.io.File; import java.util.List; import java.util.Properties; +import org.apache.maven.MavenTestCase; +import org.apache.maven.model.Goal; +import org.apache.maven.model.Plugin; +import org.apache.maven.project.MavenProject; + /** * @author Jason van Zyl * @version $Id$ @@ -46,9 +35,9 @@ public class CanonicalProjectBuilderTest public void testProjectBuilder() throws Exception { - File f = new File( basedir, "src/test/resources/canonical-pom.xml" ); + File f = getTestFile( "src/test/resources/canonical-pom.xml" ); - MavenProject project = projectBuilder.build( f ); + MavenProject project = projectBuilder.build( getMavenLocalHome(), f ); // ---------------------------------------------------------------------- // Top-level elements diff --git a/maven-core/src/test/java/org/apache/maven/project/inheritance/ProjectInheritanceTestCase.java b/maven-core/src/test/java/org/apache/maven/project/inheritance/ProjectInheritanceTestCase.java index b38aedf157..e7776e4a10 100644 --- a/maven-core/src/test/java/org/apache/maven/project/inheritance/ProjectInheritanceTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/inheritance/ProjectInheritanceTestCase.java @@ -31,18 +31,16 @@ public abstract class ProjectInheritanceTestCase projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); - System.setProperty( "maven.repo.local", getLocalRepository() ); + System.setProperty( "maven.repo.local", getLocalRepository().getAbsolutePath() ); } - private String getLocalRepository() + private File getLocalRepository() { - return new File( basedir, "src/test/resources/inheritance-repo/" + getTestSeries() ).getPath(); + return getTestFile( "src/test/resources/inheritance-repo/" + getTestSeries() ); } protected File projectFile( String name ) { - File f = new File( "src/test/resources/inheritance-repo/" + getTestSeries() + "/maven/poms", name + "-1.0.pom" ); - - return new File( basedir, f.getPath() ); + return new File( getLocalRepository(), "/maven/poms/" + name + "-1.0.pom" ); } } diff --git a/maven-core/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java b/maven-core/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java index 31d76d2880..34c3300360 100644 --- a/maven-core/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/inheritance/t00/ProjectInheritanceTest.java @@ -36,7 +36,7 @@ public class ProjectInheritanceTest public void testProjectInheritance() throws Exception { - MavenProject p4 = projectBuilder.build( projectFile( "p4" ) ); + MavenProject p4 = projectBuilder.build( getMavenLocalHome(), projectFile( "p4" ) ); assertEquals( "p4", p4.getName() ); diff --git a/maven-core/src/test/java/org/apache/maven/project/inheritance/t01/ProjectInheritanceTest.java b/maven-core/src/test/java/org/apache/maven/project/inheritance/t01/ProjectInheritanceTest.java index 493c64db57..878eedc6e6 100644 --- a/maven-core/src/test/java/org/apache/maven/project/inheritance/t01/ProjectInheritanceTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/inheritance/t01/ProjectInheritanceTest.java @@ -38,7 +38,7 @@ public class ProjectInheritanceTest // Check p0 value for org name // ---------------------------------------------------------------------- - MavenProject p0 = projectBuilder.build( projectFile( "p0" ) ); + MavenProject p0 = projectBuilder.build( getMavenLocalHome(), projectFile( "p0" ) ); assertEquals( "p0-org", p0.getOrganization().getName() ); @@ -46,7 +46,7 @@ public class ProjectInheritanceTest // Check p1 value for org name // ---------------------------------------------------------------------- - MavenProject p1 = projectBuilder.build( projectFile( "p1" ) ); + MavenProject p1 = projectBuilder.build( getMavenLocalHome(), projectFile( "p1" ) ); assertEquals( "p1-org", p1.getOrganization().getName() ); @@ -54,7 +54,7 @@ public class ProjectInheritanceTest // Check p2 value for org name // ---------------------------------------------------------------------- - MavenProject p2 = projectBuilder.build( projectFile( "p2" ) ); + MavenProject p2 = projectBuilder.build( getMavenLocalHome(), projectFile( "p2" ) ); assertEquals( "p2-org", p2.getOrganization().getName() ); @@ -62,7 +62,7 @@ public class ProjectInheritanceTest // Check p2 value for org name // ---------------------------------------------------------------------- - MavenProject p3 = projectBuilder.build( projectFile( "p3" ) ); + MavenProject p3 = projectBuilder.build( getMavenLocalHome(), projectFile( "p3" ) ); assertEquals( "p3-org", p3.getOrganization().getName() ); @@ -70,7 +70,7 @@ public class ProjectInheritanceTest // Check p4 value for org name // ---------------------------------------------------------------------- - MavenProject p4 = projectBuilder.build( projectFile( "p4" ) ); + MavenProject p4 = projectBuilder.build( getMavenLocalHome(), projectFile( "p4" ) ); assertEquals( "p4-org", p4.getOrganization().getName() ); }