From 465ac5470960b6c1cc07ec32c8041db9e9f5bbe6 Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Thu, 11 Sep 2008 08:27:13 +0000 Subject: [PATCH] Dumped most of project restorer code. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@694172 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/project/MavenProjectRestorer.java | 427 +----------------- 1 file changed, 6 insertions(+), 421 deletions(-) diff --git a/maven-project/src/main/java/org/apache/maven/project/MavenProjectRestorer.java b/maven-project/src/main/java/org/apache/maven/project/MavenProjectRestorer.java index b74495293b..b1adf310ec 100644 --- a/maven-project/src/main/java/org/apache/maven/project/MavenProjectRestorer.java +++ b/maven-project/src/main/java/org/apache/maven/project/MavenProjectRestorer.java @@ -64,7 +64,7 @@ final class MavenProjectRestorer { return; } - + /* restoreBuildRoots( project, config ); if ( project.getBuild() != null ) { @@ -77,7 +77,7 @@ final class MavenProjectRestorer { restoreDynamicState( executionProject, config ); } - + */ project.setConcrete( false ); } @@ -89,439 +89,24 @@ final class MavenProjectRestorer return; } - Build build = project.getBuild(); - if ( build != null ) - { - initResourceMergeIds( build.getResources() ); - initResourceMergeIds( build.getTestResources() ); - } - Model model = ModelUtils.cloneModel( project.getModel() ); File basedir = project.getBasedir(); - model = modelInterpolator.interpolate( model, basedir, config, getLogger().isDebugEnabled() ); - - List originalInterpolatedCompileSourceRoots = - interpolateListOfStrings( project.getCompileSourceRoots(), model, project.getBasedir(), config ); - - project.preserveCompileSourceRoots( originalInterpolatedCompileSourceRoots ); - - project.setCompileSourceRoots( originalInterpolatedCompileSourceRoots == null - ? null - : translateListOfPaths( originalInterpolatedCompileSourceRoots, basedir ) ); - - List originalInterpolatedTestCompileSourceRoots = - interpolateListOfStrings( project.getTestCompileSourceRoots(), model, project.getBasedir(), config ); - - project.preserveTestCompileSourceRoots( originalInterpolatedTestCompileSourceRoots ); - project.setTestCompileSourceRoots( originalInterpolatedTestCompileSourceRoots == null - ? null - : translateListOfPaths( originalInterpolatedTestCompileSourceRoots, basedir ) ); - - List originalInterpolatedScriptSourceRoots = - interpolateListOfStrings( project.getScriptSourceRoots(), model, project.getBasedir(), config ); - - project.preserveScriptSourceRoots( originalInterpolatedScriptSourceRoots ); - project.setScriptSourceRoots( originalInterpolatedScriptSourceRoots == null - ? null - : translateListOfPaths( originalInterpolatedScriptSourceRoots, basedir ) ); - Model model2 = ModelUtils.cloneModel( model ); - pathTranslator.alignToBaseDirectory( model, basedir ); - project.preserveBuild( model2.getBuild() ); + project.setBuild( model.getBuild() ); - calculateConcreteProjectReferences( project, config ); - + // + /* MavenProject executionProject = project.getExecutionProject(); if ( executionProject != null && executionProject != project ) { calculateConcreteState( executionProject, config ); } - + */ project.setConcrete( true ); } - - - private void restoreDynamicProjectReferences( MavenProject project, ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - Map projectRefs = project.getProjectReferences(); - if ( projectRefs != null ) - { - for ( Iterator it = projectRefs.values().iterator(); it.hasNext(); ) - { - MavenProject projectRef = (MavenProject) it.next(); - restoreDynamicState( projectRef, config ); - } - } - } - - private void restoreBuildRoots( MavenProject project, ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - project.setCompileSourceRoots( restoreListOfStrings( project.getDynamicCompileSourceRoots(), - project.getOriginalInterpolatedCompileSourceRoots(), - project.getCompileSourceRoots(), project, config ) ); - - project.setTestCompileSourceRoots( restoreListOfStrings( project.getDynamicTestCompileSourceRoots(), - project.getOriginalInterpolatedTestCompileSourceRoots(), - project.getTestCompileSourceRoots(), project, - config ) ); - - project.setScriptSourceRoots( restoreListOfStrings( project.getDynamicScriptSourceRoots(), - project.getOriginalInterpolatedScriptSourceRoots(), - project.getScriptSourceRoots(), project, config ) ); - - project.clearRestorableRoots(); - } - - private void restoreModelBuildSection( MavenProject project, ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - Build changedBuild = project.getBuild(); - Build dynamicBuild = project.getDynamicBuild(); - Build originalInterpolatedBuild = project.getOriginalInterpolatedBuild(); - - dynamicBuild.setResources( restoreResources( dynamicBuild.getResources(), - originalInterpolatedBuild.getResources(), - changedBuild.getResources(), project, config ) ); - - dynamicBuild.setTestResources( restoreResources( dynamicBuild.getTestResources(), - originalInterpolatedBuild.getTestResources(), - changedBuild.getTestResources(), project, config ) ); - - dynamicBuild.setFilters( restoreListOfStrings( dynamicBuild.getFilters(), - originalInterpolatedBuild.getFilters(), - changedBuild.getFilters(), project, config ) ); - - dynamicBuild.setFinalName( restoreString( dynamicBuild.getFinalName(), originalInterpolatedBuild.getFinalName(), - changedBuild.getFinalName(), project, config ) ); - - dynamicBuild.setDefaultGoal( restoreString( dynamicBuild.getDefaultGoal(), - originalInterpolatedBuild.getDefaultGoal(), - changedBuild.getDefaultGoal(), project, config ) ); - - dynamicBuild.setSourceDirectory( restoreString( dynamicBuild.getSourceDirectory(), - originalInterpolatedBuild.getSourceDirectory(), - changedBuild.getSourceDirectory(), project, config ) ); - - dynamicBuild.setTestSourceDirectory( restoreString( dynamicBuild.getTestSourceDirectory(), - originalInterpolatedBuild.getTestSourceDirectory(), - changedBuild.getTestSourceDirectory(), project, config ) ); - - dynamicBuild.setScriptSourceDirectory( restoreString( dynamicBuild.getScriptSourceDirectory(), - originalInterpolatedBuild.getScriptSourceDirectory(), - changedBuild.getScriptSourceDirectory(), project, - config ) ); - - dynamicBuild.setOutputDirectory( restoreString( dynamicBuild.getOutputDirectory(), - originalInterpolatedBuild.getOutputDirectory(), - changedBuild.getOutputDirectory(), project, config ) ); - - dynamicBuild.setTestOutputDirectory( restoreString( dynamicBuild.getTestOutputDirectory(), - originalInterpolatedBuild.getTestOutputDirectory(), - changedBuild.getTestOutputDirectory(), project, config ) ); - - dynamicBuild.setDirectory( restoreString( dynamicBuild.getDirectory(), originalInterpolatedBuild.getDirectory(), - changedBuild.getDirectory(), project, config ) ); - - project.setBuild( dynamicBuild ); - - project.clearRestorableBuild(); - } - - private List interpolateListOfStrings( List originalStrings, Model model, File projectDir, - ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - if ( originalStrings == null ) - { - return null; - } - - List result = new ArrayList(); - - for ( Iterator it = originalStrings.iterator(); it.hasNext(); ) - { - String original = (String) it.next(); - String interpolated = - modelInterpolator.interpolate( original, model, projectDir, config, getLogger().isDebugEnabled() ); - - result.add( interpolated ); - } - - return result; - } - - private String restoreString( String originalString, String originalInterpolatedString, String changedString, - MavenProject project, ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - if ( originalString == null ) - { - return changedString; - } - else if ( changedString == null ) - { - return originalString; - } - - Model model = project.getModel(); - - String relativeChangedString; - if ( project.getBasedir() != null ) - { - relativeChangedString = pathTranslator.unalignFromBaseDirectory( changedString, project.getBasedir() ); - } - else - { - relativeChangedString = changedString; - } - - String interpolatedOriginal = modelInterpolator.interpolate( originalString, model, project.getBasedir(), - config, getLogger().isDebugEnabled() ); - String interpolatedOriginal2 = modelInterpolator.interpolate( originalInterpolatedString, model, - project.getBasedir(), config, - getLogger().isDebugEnabled() ); - - String interpolatedChanged = modelInterpolator.interpolate( changedString, model, project.getBasedir(), config, - getLogger().isDebugEnabled() ); - String relativeInterpolatedChanged = modelInterpolator.interpolate( relativeChangedString, model, - project.getBasedir(), config, - getLogger().isDebugEnabled() ); - - if ( interpolatedOriginal.equals( interpolatedChanged ) || interpolatedOriginal2.equals( interpolatedChanged ) ) - { - return originalString; - } - else if ( interpolatedOriginal.equals( relativeInterpolatedChanged ) || - interpolatedOriginal2.equals( relativeInterpolatedChanged ) ) - { - return originalString; - } - - return relativeChangedString; - } - - private List restoreListOfStrings( List originalStrings, List originalInterpolatedStrings, List changedStrings, - MavenProject project, ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - if ( originalStrings == null ) - { - return changedStrings; - } - else if ( changedStrings == null ) - { - return originalStrings; - } - - List result = new ArrayList(); - - Map orig = new HashMap(); - for ( int idx = 0; idx < originalStrings.size(); idx++ ) - { - String[] permutations = new String[2]; - - permutations[0] = (String) originalInterpolatedStrings.get( idx ); - permutations[1] = (String) originalStrings.get( idx ); - - orig.put( permutations[0], permutations ); - } - - for ( Iterator it = changedStrings.iterator(); it.hasNext(); ) - { - String changedString = (String) it.next(); - String relativeChangedString; - if ( project.getBasedir() != null ) - { - relativeChangedString = pathTranslator.unalignFromBaseDirectory( changedString, project.getBasedir() ); - } - else - { - relativeChangedString = changedString; - } - - String interpolated = modelInterpolator.interpolate( changedString, project.getModel(), - project.getBasedir(), config, - getLogger().isDebugEnabled() ); - - String relativeInterpolated = modelInterpolator.interpolate( relativeChangedString, project.getModel(), - project.getBasedir(), config, - getLogger().isDebugEnabled() ); - - String[] original = (String[]) orig.get( interpolated ); - if ( original == null ) - { - original = (String[]) orig.get( relativeInterpolated ); - } - - if ( original == null ) - { - result.add( relativeChangedString ); - } - else - { - result.add( original[1] ); - } - } - - return result; - } - - // TODO: Convert this to use the mergeId on each resource... - private List restoreResources( List originalResources, List originalInterpolatedResources, - List changedResources, MavenProject project, - ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - if ( originalResources == null || changedResources == null ) - { - return originalResources; - } - - List result = new ArrayList(); - - Map originalResourcesByMergeId = new HashMap(); - for ( int idx = 0; idx < originalResources.size(); idx++ ) - { - Resource[] permutations = new Resource[2]; - - permutations[0] = originalInterpolatedResources.get( idx ); - permutations[1] = originalResources.get( idx ); - - originalResourcesByMergeId.put( permutations[0].getMergeId(), permutations ); - } - - for ( Resource resource : changedResources ) - { - String mergeId = resource.getMergeId(); - if ( mergeId == null || !originalResourcesByMergeId.containsKey( mergeId ) ) - { - result.add( resource ); - } - else - { - Resource originalInterpolatedResource = originalResourcesByMergeId.get( mergeId )[0]; - Resource originalResource = originalResourcesByMergeId.get( mergeId )[1]; - - String dir = modelInterpolator.interpolate( resource.getDirectory(), project.getModel(), - project.getBasedir(), config, - getLogger().isDebugEnabled() ); - String oDir = originalInterpolatedResource.getDirectory(); - - if ( !dir.equals( oDir ) ) - { - originalResource.setDirectory( - pathTranslator.unalignFromBaseDirectory( dir, project.getBasedir() ) ); - } - - if ( resource.getTargetPath() != null ) - { - String target = modelInterpolator.interpolate( resource.getTargetPath(), project.getModel(), - project.getBasedir(), config, - getLogger().isDebugEnabled() ); - - String oTarget = originalInterpolatedResource.getTargetPath(); - - if ( !target.equals( oTarget ) ) - { - originalResource.setTargetPath( - pathTranslator.unalignFromBaseDirectory( target, project.getBasedir() ) ); - } - } - - originalResource.setFiltering( resource.isFiltering() ); - - originalResource.setExcludes( collectRestoredListOfPatterns( resource.getExcludes(), - originalResource.getExcludes(), - originalInterpolatedResource.getExcludes() ) ); - - originalResource.setIncludes( collectRestoredListOfPatterns( resource.getIncludes(), - originalResource.getIncludes(), - originalInterpolatedResource.getIncludes() ) ); - - result.add( originalResource ); - } - } - - return result; - } - - private List collectRestoredListOfPatterns( List patterns, List originalPatterns, - List originalInterpolatedPatterns ) - { - LinkedHashSet collectedPatterns = new LinkedHashSet(); - - collectedPatterns.addAll( originalPatterns ); - - for ( String pattern : patterns ) - { - if ( !originalInterpolatedPatterns.contains( pattern ) ) - { - collectedPatterns.add( pattern ); - } - } - - return (List) ( collectedPatterns.isEmpty() - ? Collections.emptyList() - : new ArrayList( collectedPatterns ) ); - } - - - private void initResourceMergeIds( List resources ) - { - if ( resources != null ) - { - for ( Resource resource : resources ) - { - resource.initMergeId(); - } - } - } - - private void calculateConcreteProjectReferences( MavenProject project, ProjectBuilderConfiguration config ) - throws ModelInterpolationException - { - Map projectRefs = project.getProjectReferences(); - - if ( projectRefs != null ) - { - for ( Iterator it = projectRefs.values().iterator(); it.hasNext(); ) - { - MavenProject reference = (MavenProject) it.next(); - calculateConcreteState( reference, config ); - } - } - } - - private List translateListOfPaths( List paths, File basedir ) - { - if ( paths == null ) - { - return null; - } - else if ( basedir == null ) - { - return paths; - } - - List result = new ArrayList( paths.size() ); - for ( Iterator it = paths.iterator(); it.hasNext(); ) - { - String path = (String) it.next(); - - String aligned = pathTranslator.alignToBaseDirectory( path, basedir ); - - result.add( aligned ); - } - - return result; - } - - }