1 package org.apache.maven.archiva.plugins.dev.testgen;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.iterators.EnumerationIterator;
25 import org.apache.maven.archiva.plugins.dev.functors.MatchingDependencyPredicate;
26 import org.apache.maven.artifact.InvalidArtifactRTException;
27 import org.apache.maven.model.Dependency;
28 import org.apache.maven.model.DistributionManagement;
29 import org.apache.maven.model.Exclusion;
30 import org.apache.maven.model.Model;
31 import org.apache.maven.model.Relocation;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.plexus.util.StringUtils;
37 import java.io.FileNotFoundException;
38 import java.io.PrintWriter;
39 import java.util.HashSet;
40 import java.util.Iterator;
44 * MemoryRepositoryCreator
46 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49 public class MemoryRepositoryCreator
50 extends AbstractCreator
52 private File outputFile;
54 private PrintWriter out;
56 private int modelsProcessed = 0;
58 public void create( String classPrefix )
59 throws MojoExecutionException
61 getLog().info( "Generating " + classPrefix + "MemoryRepository.java ..." );
62 getLog().info( "Please be patient, this can take a few minutes ..." );
66 outputFile = new File( outputDir, classPrefix + "MemoryRepository.java" );
69 out = new PrintWriter( outputFile );
71 catch ( FileNotFoundException e )
73 throw new MojoExecutionException( "Unable to open file " + outputFile.getName() + " for output: "
74 + e.getMessage(), e );
79 out.println( "package org.apache.maven.archiva.dependency.graph;" );
85 out.println( "import org.apache.maven.archiva.model.ArchivaProjectModel;" );
86 out.println( "import org.apache.maven.archiva.model.Dependency;" );
87 out.println( "import org.apache.maven.archiva.model.VersionedReference;" );
90 String projectKey = toKey( project.getModel() );
92 writeJavadoc( classPrefix + "MemoryRepository", projectKey );
95 out.println( "public class " + classPrefix + "MemoryRepository" );
96 out.println( " extends AbstractMemoryRepository" );
111 private void writeJavadoc( String classname, String projectKey )
113 out.println( "/**" );
114 out.println( " * " + classname );
115 out.println( " * " );
116 out.println( " * MemoryRepository for testing <code>" + projectKey + "</code>" );
118 out.println( " * Generated by <code>archivadev:generate-dependency-tests</code> plugin" );
119 out.println( " * @version $Id$" );
120 out.println( " */" );
123 private void writeTest()
125 out.println( " public void initialize()" );
127 out.println( " ArchivaProjectModel model;" );
128 out.println( " Dependency dep;" );
131 Set seenModels = new HashSet();
133 writeModel( seenModels, project.getModel() );
136 "Processing done: Processed " + modelsProcessed + " models (" + seenModels.size()
142 private void writeModel( Set seenModels, Model model )
144 String projectKey = toKey( model );
148 if ( ( modelsProcessed % 100 ) == 0 )
151 .info( "Processed " + modelsProcessed + " models (" + seenModels.size() + " unique) ..." );
154 Relocation relocation = null;
156 if ( seenModels.contains( projectKey ) )
161 seenModels.add( projectKey );
163 out.println( " model = toModel( \"" + projectKey + "\" );" );
165 if ( model.getParent() != null )
167 String parentKey = toKey( model.getParent() );
168 out.println( " model.setParentProject( toParent( \"" + parentKey + "\" ) );" );
171 if ( isNotEmpty( model.getDependencies() ) )
173 Iterator it = model.getDependencies().iterator();
174 while ( it.hasNext() )
176 Dependency dep = applyDepMan( (Dependency) it.next(), model );
178 writeAddDependency( "addDependency", dep );
182 if ( ( model.getDependencyManagement() != null )
183 && isNotEmpty( model.getDependencyManagement().getDependencies() ) )
185 Iterator it = model.getDependencyManagement().getDependencies().iterator();
186 while ( it.hasNext() )
188 Dependency dep = (Dependency) it.next();
190 writeAddDependency( "addDependencyManagement", dep );
194 if ( isNotEmpty( model.getProperties() ) )
196 Iterator it = new EnumerationIterator( model.getProperties().keys() );
197 while ( it.hasNext() )
199 String key = (String) it.next();
200 String value = model.getProperties().getProperty( key );
201 out.println( " model.addProperty( \"" + key + "\", \"" + value + "\" );" );
205 if ( model.getDistributionManagement() != null )
207 DistributionManagement distMgmt = model.getDistributionManagement();
209 if ( distMgmt.getRelocation() != null )
211 relocation = distMgmt.getRelocation();
213 out.println( " model.setRelocation( new VersionedReference() );" );
214 if ( StringUtils.isNotEmpty( relocation.getGroupId() ) )
216 out.println( " model.getRelocation().setGroupId( \"" + relocation.getGroupId() + "\" );" );
218 if ( StringUtils.isNotEmpty( relocation.getArtifactId() ) )
221 .println( " model.getRelocation().setArtifactId( \"" + relocation.getArtifactId()
224 if ( StringUtils.isNotEmpty( relocation.getVersion() ) )
226 out.println( " model.getRelocation().setVersion( \"" + relocation.getVersion() + "\" );" );
231 out.println( " addModel( model );" );
234 if ( model.getParent() != null )
236 Model parentModel = getModel( model.getParent() );
237 writeModel( seenModels, parentModel );
240 if ( relocation != null )
242 Model relocatedModel = getModel( model, relocation );
243 writeModel( seenModels, relocatedModel );
246 writeModelDependencies( seenModels, projectKey, model );
247 writeModelDependencyManagement( seenModels, projectKey, model );
250 private Model getModel( Model model, Relocation relocation )
252 String groupId = relocation.getGroupId();
253 String artifactId = relocation.getArtifactId();
254 String version = relocation.getVersion();
256 // Set empty groupId.
257 if ( StringUtils.isEmpty( groupId ) )
259 groupId = model.getGroupId();
261 if ( StringUtils.isEmpty( groupId ) )
263 if ( model.getParent() == null )
265 throw new IllegalStateException( "BAD POM: GroupId for relocation in " + toKey( model )
266 + " cannot be determined, as there is no Parent Pom reference." );
269 groupId = model.getParent().getGroupId();
273 // Set empty artifactId.
274 if ( StringUtils.isEmpty( artifactId ) )
276 artifactId = model.getArtifactId();
278 if ( StringUtils.isEmpty( artifactId ) )
280 if ( model.getParent() == null )
282 throw new IllegalStateException( "BAD POM: ArtifactId for relocation in " + toKey( model )
283 + " cannot be determined, as there is no Parent Pom reference." );
286 artifactId = model.getParent().getArtifactId();
290 // Set empty version.
291 if ( StringUtils.isEmpty( version ) )
293 version = model.getVersion();
295 if ( StringUtils.isEmpty( version ) )
297 if ( model.getParent() == null )
299 throw new IllegalStateException( "BAD POM: version for relocation in " + toKey( model )
300 + " cannot be determined, as there is no Parent Pom reference." );
303 version = model.getParent().getVersion();
307 return getModel( groupId, artifactId, version, "pom" );
310 private void writeAddDependency( String addMethod, Dependency dep )
312 boolean useShortForm = true;
313 String depKey = toKey( dep );
315 useShortForm = isEmpty( dep.getExclusions() ) && !dep.isOptional();
317 String scopePart = "";
319 if ( isNotBlank( dep.getScope() ) )
321 scopePart = ", \"" + dep.getScope() + "\"";
326 out.println( " model." + addMethod + "( toDependency( \"" + depKey + "\"" + scopePart + " ) );" );
330 out.println( " dep = toDependency( \"" + depKey + "\"" + scopePart + " );" );
332 if ( isNotEmpty( dep.getExclusions() ) )
334 Iterator it = dep.getExclusions().iterator();
335 while ( it.hasNext() )
337 Exclusion exclusion = (Exclusion) it.next();
338 String exkey = toKey( exclusion );
339 out.println( " addExclusion( dep, \"" + exkey + "\" );" );
343 if ( dep.isOptional() )
345 out.println( " dep.setOptional( true );" );
348 out.println( " model." + addMethod + "( dep );" );
351 private void writeModelDependencies( Set seenModels, String projectKey, Model model )
353 if ( model.getDependencies() == null )
358 Iterator it = model.getDependencies().iterator();
360 while ( it.hasNext() )
362 Dependency dep = applyDepMan( (Dependency) it.next(), model );
364 Model rawModel = getModel( dep );
368 writeModel( seenModels, rawModel );
370 catch ( InvalidArtifactRTException e )
373 "Encountered invalid dependency/artifact during [" + projectKey + "] : "
374 + e.getMessage(), e );
380 private void writeModelDependencyManagement( Set seenModels, String projectKey, Model model )
382 if ( model.getDependencyManagement() == null )
387 Iterator it = model.getDependencyManagement().getDependencies().iterator();
389 while ( it.hasNext() )
391 Dependency dep = (Dependency) it.next();
393 Model rawModel = getModel( dep );
397 writeModel( seenModels, rawModel );
399 catch ( InvalidArtifactRTException e )
402 "Encountered invalid dependency/artifact during [" + projectKey + "] : "
403 + e.getMessage(), e );
409 private Dependency applyDepMan( Dependency dependency, Model model )
411 if ( model.getDependencyManagement() != null )
413 if ( model.getDependencyManagement().getDependencies() != null )
415 // Attempt to find matching dep.
416 Predicate matchingDep = new MatchingDependencyPredicate( dependency );
417 Dependency depman = (Dependency) CollectionUtils.find( model.getDependencyManagement()
418 .getDependencies(), matchingDep );
420 if ( depman != null )
422 dependency.setVersion( depman.getVersion() );
423 dependency.setScope( StringUtils.defaultString( depman.getScope(), dependency.getScope() ) );
431 if ( model.getParent() != null )
433 Model parentModel = getModel( model.getParent() );
434 return applyDepMan( dependency, parentModel );