View Javadoc

1   package org.apache.maven.plugin.assembly.utils;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.project.MavenProject;
24  import org.codehaus.plexus.logging.Logger;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.LinkedHashSet;
31  import java.util.List;
32  import java.util.Set;
33  
34  /**
35   * @version $Id: ProjectUtils.java 1401695 2012-10-24 14:01:38Z dennisl $
36   */
37  public final class ProjectUtils
38  {
39  
40      private ProjectUtils()
41      {
42      }
43  
44      public static String getClassifier( Artifact artifact )
45      {
46          String classifier = artifact.getClassifier();
47          if ( classifier != null && classifier.length() == 0 )
48          {
49              classifier = null;
50          }
51          return classifier;
52      }
53  
54      public static Set<MavenProject> getProjectModules( final MavenProject project,
55                                                         final List<MavenProject> reactorProjects,
56                                                         final boolean includeSubModules, final Logger logger )
57          throws IOException
58      {
59          final Set<MavenProject> singleParentSet = Collections.singleton( project );
60  
61          final Set<MavenProject> moduleCandidates = new LinkedHashSet<MavenProject>( reactorProjects );
62  
63          final Set<MavenProject> modules = new LinkedHashSet<MavenProject>();
64  
65          // we temporarily add the master project to the modules set, since this
66          // set is pulling double duty as a set of
67          // potential module parents in the tree rooted at the master
68          // project...this allows us to use the same looping
69          // algorithm below to discover both direct modules of the master project
70          // AND modules of those direct modules.
71          modules.add( project );
72  
73          int changed = 0;
74  
75          do
76          {
77              changed = 0;
78  
79              for ( final Iterator<MavenProject> candidateIterator = moduleCandidates.iterator(); candidateIterator.hasNext(); )
80              {
81                  final MavenProject moduleCandidate = candidateIterator.next();
82  
83                  if ( moduleCandidate.getFile() == null )
84                  {
85                      logger.warn( "Cannot compute whether " + moduleCandidate.getId() + " is a module of: "
86                                      + project.getId()
87                                      + "; it does not have an associated POM file on the local filesystem." );
88                      continue;
89                  }
90  
91                  Set<MavenProject> currentPotentialParents;
92                  if ( includeSubModules )
93                  {
94                      currentPotentialParents = new LinkedHashSet<MavenProject>( modules );
95                  }
96                  else
97                  {
98                      currentPotentialParents = singleParentSet;
99                  }
100 
101                 for ( final Iterator<MavenProject> parentIterator = currentPotentialParents.iterator(); parentIterator.hasNext(); )
102                 {
103                     final MavenProject potentialParent = parentIterator.next();
104 
105                     if ( potentialParent.getFile() == null )
106                     {
107                         logger.warn( "Cannot use: " + moduleCandidate.getId()
108                                         + " as a potential module-parent while computing the module set for: "
109                                         + project.getId()
110                                         + "; it does not have an associated POM file on the local filesystem." );
111                         continue;
112                     }
113 
114                     // if this parent has an entry for the module candidate in
115                     // the path adjustments map, it's a direct
116                     // module of that parent.
117                     if ( projectContainsModule( potentialParent, moduleCandidate ) )
118                     {
119                         // add the candidate to the list of modules (and
120                         // potential parents)
121                         modules.add( moduleCandidate );
122 
123                         // remove the candidate from the candidate pool, because
124                         // it's been verified.
125                         candidateIterator.remove();
126 
127                         // increment the change counter, to show that we
128                         // verified a new module on this pass.
129                         changed++;
130                     }
131                 }
132             }
133         }
134         while ( changed != 0 );
135 
136         // remove the master project from the modules set, now that we're done
137         // using it as a set of potential module
138         // parents...
139         modules.remove( project );
140 
141         return modules;
142     }
143 
144     private static boolean projectContainsModule( final MavenProject mainProject, final MavenProject moduleProject )
145         throws IOException
146     {
147         @SuppressWarnings( "unchecked" )
148         final List<String> modules = mainProject.getModules();
149         final File basedir = mainProject.getBasedir();
150 
151         final File moduleFile = moduleProject.getFile()
152                                              .getCanonicalFile();
153 
154         File moduleBasedir = moduleProject.getBasedir();
155 
156         if ( moduleBasedir == null )
157         {
158             if ( moduleFile != null )
159             {
160                 moduleBasedir = moduleFile.getParentFile();
161             }
162 
163             if ( moduleBasedir == null )
164             {
165                 moduleBasedir = new File( "." );
166             }
167         }
168 
169         moduleBasedir = moduleBasedir.getCanonicalFile();
170 
171         for ( final Iterator<String> it = modules.iterator(); it.hasNext(); )
172         {
173             final String moduleSubpath = it.next();
174 
175             final File moduleDir = new File( basedir, moduleSubpath ).getCanonicalFile();
176 
177             if ( moduleDir.equals( moduleFile ) || moduleDir.equals( moduleBasedir ) )
178             {
179                 return true;
180             }
181         }
182 
183         return false;
184     }
185 
186 }