From 65b542741b82c3e5499a667158959c880698ac83 Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Thu, 21 Apr 2005 02:17:51 +0000 Subject: [PATCH] o Added --force CLI support o Added blacklistedPatterns to configuration file, to enhance the exclusion filter during artifact discovery with a set of non-copy-able files. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163997 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/tools/repoclean/Main.java | 34 +++++++- .../tools/repoclean/RepositoryCleaner.java | 86 ++++++++++--------- .../RepositoryCleanerConfiguration.java | 12 +++ .../discover/ArtifactDiscoverer.java | 10 +-- .../discover/DefaultArtifactDiscoverer.java | 19 +++- .../discover/LegacyArtifactDiscoverer.java | 67 +++++++++++---- 6 files changed, 162 insertions(+), 66 deletions(-) diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/Main.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/Main.java index b85a5becc2..3dadd0b96f 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/Main.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/Main.java @@ -30,9 +30,14 @@ */ public class Main { + + public static final String FORCE_ARG = "--force"; public static void main( String[] args ) { + boolean force = false; + String configFile = null; + if ( args.length < 1 ) { printUsage(); @@ -48,10 +53,36 @@ else if ( "-template".equals( args[0] ) ) printTemplate(); System.exit( 0 ); } + // up the ante, and let's try to see if there's a --force option. + else if ( args.length == 2 ) + { + if(FORCE_ARG.equals(args[0])) + { + force = true; + configFile = args[1]; + } + else if(FORCE_ARG.equals(args[1])) + { + force = true; + configFile = args[0]; + } + else + { + System.out.println("Invalid argument list: \'" + args[0] + " " + args[1]); + printUsage(); + System.exit(1); + } + } + else + { + configFile = args[0]; + } try { - RepositoryCleanerConfiguration config = buildConfig( args[0] ); + RepositoryCleanerConfiguration config = buildConfig( configFile ); + + config.setForce(force); launch( config ); @@ -108,6 +139,7 @@ private static RepositoryCleanerConfiguration buildConfig( String configPath ) config.setTargetRepositoryPath( props.getProperty( "targetRepositoryPath" ) ); config.setTargetRepositoryLayout( props.getProperty( "targetRepositoryLayout", "default" ) ); config.setReportsPath( props.getProperty( "reportsPath" ) ); + config.setBlacklistedPatterns( props.getProperty( "blacklistedPatterns" ) ); config.setReportOnly( Boolean.valueOf( props.getProperty( "reportOnly" ) ).booleanValue() ); config.setMailErrorReport( Boolean.valueOf( props.getProperty( "errorReport.mailOnError", "false") ).booleanValue() ); diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java index 6a73701a7a..930e1fed0d 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleaner.java @@ -67,9 +67,9 @@ public class RepositoryCleaner private ArtifactRepositoryLayout bridgingLayout; private MailSender mailSender; - + private ArtifactIndexer artifactIndexer; - + private ArtifactConstructionSupport artifactConstructionSupport = new ArtifactConstructionSupport(); private PlexusContainer container; @@ -101,9 +101,8 @@ public void cleanRepository( RepositoryCleanerConfiguration configuration ) List artifacts = null; try { - artifactDiscoverer = (ArtifactDiscoverer) container.lookup( - ArtifactDiscoverer.ROLE, - configuration.getSourceRepositoryLayout() ); + artifactDiscoverer = (ArtifactDiscoverer) container.lookup( ArtifactDiscoverer.ROLE, configuration + .getSourceRepositoryLayout() ); if ( logger.isInfoEnabled() ) { @@ -112,7 +111,8 @@ public void cleanRepository( RepositoryCleanerConfiguration configuration ) try { - artifacts = artifactDiscoverer.discoverArtifacts( sourceRepositoryBase, repoReporter ); + artifacts = artifactDiscoverer.discoverArtifacts( sourceRepositoryBase, repoReporter, + configuration.getBlacklistedPatterns() ); } catch ( Exception e ) { @@ -134,16 +134,16 @@ public void cleanRepository( RepositoryCleanerConfiguration configuration ) ArtifactRepositoryLayout targetLayout = null; try { - sourceLayout = (ArtifactRepositoryLayout) container.lookup( - ArtifactRepositoryLayout.ROLE, - configuration.getSourceRepositoryLayout() ); + sourceLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, + configuration + .getSourceRepositoryLayout() ); ArtifactRepository sourceRepo = new ArtifactRepository( "source", "file://" + sourceRepositoryBase.getAbsolutePath(), sourceLayout ); - targetLayout = (ArtifactRepositoryLayout) container.lookup( - ArtifactRepositoryLayout.ROLE, - configuration.getTargetRepositoryLayout() ); + targetLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, + configuration + .getTargetRepositoryLayout() ); ArtifactRepository targetRepo = new ArtifactRepository( "target", "file://" + targetRepositoryBase.getAbsolutePath(), targetLayout ); @@ -154,7 +154,7 @@ public void cleanRepository( RepositoryCleanerConfiguration configuration ) } artifactIndexer.writeAritfactIndex( artifacts, targetRepositoryBase ); - + rewriteArtifactsAndPoms( artifacts, sourceRepo, targetRepo, configuration, reportsBase, sourceRepositoryBase, targetRepositoryBase, repoReporter ); } @@ -179,7 +179,8 @@ public void cleanRepository( RepositoryCleanerConfiguration configuration ) if ( repoReporter.hasWarning() && logger.isWarnEnabled() ) { - logger.warn( "Warning encountered while rewriting one or more artifacts from source repository to target repository." ); + logger + .warn( "Warning encountered while rewriting one or more artifacts from source repository to target repository." ); } if ( repoReporter.hasError() ) @@ -245,12 +246,12 @@ private void rewriteArtifactsAndPoms( List artifacts, ArtifactRepository sourceR Logger logger = getLogger(); ArtifactPomRewriter artifactPomRewriter = null; - + try { logger.info( "Rewriting up to " + artifacts.size() + " artifacts (Should be " + ( artifacts.size() * 2 ) + " rewrites including POMs)." ); - + int actualRewriteCount = 0; for ( Iterator it = artifacts.iterator(); it.hasNext(); ) { @@ -273,10 +274,10 @@ private void rewriteArtifactsAndPoms( List artifacts, ArtifactRepository sourceR boolean targetMissingOrOlder = !artifactTarget.exists() || artifactTarget.lastModified() < artifactSource.lastModified(); - if ( artifactSource.exists() && targetMissingOrOlder ) + if ( artifactSource.exists() && ( configuration.force() || targetMissingOrOlder ) ) { actualRewriteCount++; - + try { if ( !configuration.reportOnly() ) @@ -334,21 +335,21 @@ private void rewriteArtifactsAndPoms( List artifacts, ArtifactRepository sourceR { ArtifactMetadata pom = new ProjectMetadata( artifact ); - artifactPomRewriter = (ArtifactPomRewriter) container.lookup( - ArtifactPomRewriter.ROLE, - configuration.getSourcePomVersion() ); + artifactPomRewriter = (ArtifactPomRewriter) container.lookup( ArtifactPomRewriter.ROLE, + configuration + .getSourcePomVersion() ); File sourcePom = new File( sourceRepositoryBase, sourceRepo.pathOfMetadata( pom ) ); File targetPom = new File( targetRepositoryBase, targetRepo.pathOfMetadata( pom ) ); - + File bridgedTargetPom = new File( targetRepositoryBase, bridgingLayout.pathOfMetadata( pom ) ); try { artifactPomRewriter.rewrite( artifact, sourcePom, targetPom, artifactReporter, configuration.reportOnly() ); - + bridgePomLocations( targetPom, bridgedTargetPom, artifactReporter ); } catch ( Exception e ) @@ -359,10 +360,11 @@ private void rewriteArtifactsAndPoms( List artifacts, ArtifactRepository sourceR } } - else if( !targetMissingOrOlder ) + else if ( !targetMissingOrOlder ) { - artifactReporter.warn( "Target file for artifact is present and not stale. (Artifact: \'" + artifact.getId() - + "\' in path: \'" + artifactSource + "\' with target path: " + artifactTarget + ")." ); + artifactReporter.warn( "Target file for artifact is present and not stale. (Artifact: \'" + + artifact.getId() + "\' in path: \'" + artifactSource + "\' with target path: " + + artifactTarget + ")." ); } else { @@ -389,8 +391,9 @@ else if( !targetMissingOrOlder ) } } } - - logger.info("Actual number of artifacts rewritten: " + actualRewriteCount + " (" + (actualRewriteCount * 2) + " including POMs)."); + + logger.info( "Actual number of artifacts rewritten: " + actualRewriteCount + " (" + + ( actualRewriteCount * 2 ) + " including POMs)." ); } finally { @@ -401,27 +404,29 @@ else if( !targetMissingOrOlder ) } } - private void bridgePomLocations( File targetPom, File bridgedTargetPom, Reporter reporter ) throws IOException, ReportWriteException + private void bridgePomLocations( File targetPom, File bridgedTargetPom, Reporter reporter ) + throws IOException, ReportWriteException { - if(targetPom.equals(bridgedTargetPom)) + if ( targetPom.equals( bridgedTargetPom ) ) { - reporter.warn("Cannot create legacy-compatible copy of POM at: " + targetPom + "; legacy-compatible path is the same as the converted POM itself."); + reporter.warn( "Cannot create legacy-compatible copy of POM at: " + targetPom + + "; legacy-compatible path is the same as the converted POM itself." ); } - + FileInputStream in = null; FileOutputStream out = null; - + try { - in = new FileInputStream(targetPom); - out = new FileOutputStream(bridgedTargetPom); - - IOUtil.copy(in, out); + in = new FileInputStream( targetPom ); + out = new FileOutputStream( bridgedTargetPom ); + + IOUtil.copy( in, out ); } finally { - IOUtil.close(in); - IOUtil.close(out); + IOUtil.close( in ); + IOUtil.close( out ); } } @@ -538,7 +543,8 @@ else if ( !reportsBase.isDirectory() ) return reportsBase; } - public void contextualize( Context context ) throws ContextException + public void contextualize( Context context ) + throws ContextException { this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); } diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleanerConfiguration.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleanerConfiguration.java index 654db3c719..9b425ab90c 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleanerConfiguration.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/RepositoryCleanerConfiguration.java @@ -51,6 +51,8 @@ public class RepositoryCleanerConfiguration private boolean force; + private String blacklistedPatterns; + public void setSourceRepositoryPath( String sourceRepositoryPath ) { this.sourceRepositoryPath = sourceRepositoryPath; @@ -190,4 +192,14 @@ public void setForce( boolean force ) { this.force = force; } + + public void setBlacklistedPatterns( String blacklistedPatterns ) + { + this.blacklistedPatterns = blacklistedPatterns; + } + + public String getBlacklistedPatterns() + { + return blacklistedPatterns; + } } \ No newline at end of file diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/ArtifactDiscoverer.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/ArtifactDiscoverer.java index ea481df232..f2f010a741 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/ArtifactDiscoverer.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/ArtifactDiscoverer.java @@ -25,7 +25,7 @@ public interface ArtifactDiscoverer { public static final String ROLE = ArtifactDiscoverer.class.getName(); - + public static final String[] STANDARD_DISCOVERY_EXCLUDES = { "bin/**", "reports/**", @@ -39,9 +39,9 @@ public interface ArtifactDiscoverer "**/.htaccess", "**/*.html", "**/*.asc", - "**/*.txt" - }; + "**/*.txt" }; + + List discoverArtifacts( File repositoryBase, FileReporter reporter, String blacklistedPatterns ) + throws Exception; - List discoverArtifacts( File repositoryBase, FileReporter reporter ) throws Exception; - } \ No newline at end of file diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/DefaultArtifactDiscoverer.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/DefaultArtifactDiscoverer.java index 686f9852a4..3e682075f4 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/DefaultArtifactDiscoverer.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/DefaultArtifactDiscoverer.java @@ -36,14 +36,29 @@ public class DefaultArtifactDiscoverer private ArtifactConstructionSupport artifactConstructionSupport = new ArtifactConstructionSupport(); - public List discoverArtifacts( File repositoryBase, FileReporter reporter ) + public List discoverArtifacts( File repositoryBase, FileReporter reporter, String blacklistedPatterns ) throws Exception { List artifacts = new ArrayList(); + String[] blacklisted = null; + if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 ) + { + blacklisted = blacklistedPatterns.split( "," ); + } + else + { + blacklisted = new String[0]; + } + + String[] allExcludes = new String[STANDARD_DISCOVERY_EXCLUDES.length + blacklisted.length]; + + System.arraycopy( STANDARD_DISCOVERY_EXCLUDES, 0, allExcludes, 0, STANDARD_DISCOVERY_EXCLUDES.length ); + System.arraycopy( blacklisted, 0, allExcludes, 0, blacklisted.length ); + DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir( repositoryBase ); - scanner.setExcludes( STANDARD_DISCOVERY_EXCLUDES ); + scanner.setExcludes( allExcludes ); scanner.scan(); diff --git a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/LegacyArtifactDiscoverer.java b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/LegacyArtifactDiscoverer.java index 8bd8a22308..df0c00ef45 100644 --- a/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/LegacyArtifactDiscoverer.java +++ b/sandbox/repoclean/src/main/java/org/apache/maven/tools/repoclean/discover/LegacyArtifactDiscoverer.java @@ -38,14 +38,38 @@ public class LegacyArtifactDiscoverer private ArtifactConstructionSupport artifactConstructionSupport = new ArtifactConstructionSupport(); - public List discoverArtifacts( File repositoryBase, FileReporter reporter ) + public List discoverArtifacts( File repositoryBase, FileReporter reporter, String blacklistedPatterns ) throws Exception { List artifacts = new ArrayList(); + String[] blacklisted = null; + if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 ) + { + blacklisted = blacklistedPatterns.split( "," ); + } + else + { + blacklisted = new String[0]; + } + + String[] allExcludes = null; + + if ( blacklisted != null && blacklisted.length > 0 ) + { + allExcludes = new String[STANDARD_DISCOVERY_EXCLUDES.length + blacklisted.length ]; + + System.arraycopy( STANDARD_DISCOVERY_EXCLUDES, 0, allExcludes, 0, STANDARD_DISCOVERY_EXCLUDES.length ); + System.arraycopy( blacklisted, 0, allExcludes, STANDARD_DISCOVERY_EXCLUDES.length, blacklisted.length ); + } + else + { + allExcludes = STANDARD_DISCOVERY_EXCLUDES; + } + DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir( repositoryBase ); - scanner.setExcludes( STANDARD_DISCOVERY_EXCLUDES ); + scanner.setExcludes( allExcludes ); scanner.scan(); @@ -205,20 +229,25 @@ else if ( lastAvceToken.endsWith( ".zip" ) ) tokensIterated++; } - - getLogger().debug("After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer + "\'\no Classifier Buffer: \'" + classifierBuffer + "\'\no Number of Tokens Iterated: " + tokensIterated); + + getLogger().debug( + "After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer + + "\'\no Classifier Buffer: \'" + classifierBuffer + + "\'\no Number of Tokens Iterated: " + tokensIterated ); // Now, restore the proper ordering so we can build the artifactId. Collections.reverse( avceTokenList ); - - getLogger().debug("Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + avceTokenList); - + + getLogger().debug( + "Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + + avceTokenList ); + // if we didn't find a version, then punt. Use the last token // as the version, and set the classifier empty. if ( versionBuffer.length() < 1 ) { int lastIdx = avceTokenList.size() - 1; - + versionBuffer.append( avceTokenList.get( lastIdx ) ); avceTokenList.remove( lastIdx ); @@ -226,14 +255,14 @@ else if ( lastAvceToken.endsWith( ".zip" ) ) } else { - getLogger().debug("Removing " + tokensIterated + " tokens from avce token list."); - + getLogger().debug( "Removing " + tokensIterated + " tokens from avce token list." ); + // if everything is kosher, then pop off all the classifier and // version tokens, leaving the naked artifact id in the list. avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - ( tokensIterated ) ) ); } - - getLogger().debug("Now, remainder of avce token list is:\n" + avceTokenList); + + getLogger().debug( "Now, remainder of avce token list is:\n" + avceTokenList ); StringBuffer artifactIdBuffer = new StringBuffer(); @@ -270,16 +299,16 @@ else if ( lastAvceToken.endsWith( ".zip" ) ) } getLogger().debug( - "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" - + "artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" - + version + "\'\n" + "classifier: \'" + classifierBuffer.toString() + "\'" ); + "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" + + "artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" + + version + "\'\n" + "classifier: \'" + classifierBuffer.toString() + "\'" ); Artifact result = null; if ( classifierBuffer.length() > 0 ) { - getLogger().debug("Creating artifact with classifier."); - + getLogger().debug( "Creating artifact with classifier." ); + result = artifactConstructionSupport.createArtifactWithClassifier( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type, classifierBuffer.toString() ); @@ -290,7 +319,9 @@ else if ( lastAvceToken.endsWith( ".zip" ) ) type ); } - getLogger().debug( "Resulting artifact is: " + result.getId() + " and has classifier of: " + result.getClassifier() + "\n\n" ); + getLogger().debug( + "Resulting artifact is: " + result.getId() + " and has classifier of: " + + result.getClassifier() + "\n\n" ); return result; }