View Javadoc

1   package org.apache.continuum.utils.release;
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.model.Model;
23  import org.apache.maven.model.Plugin;
24  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
25  import org.apache.maven.shared.release.versions.DefaultVersionInfo;
26  import org.apache.maven.shared.release.versions.VersionInfo;
27  import org.codehaus.plexus.util.ReaderFactory;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.codehaus.plexus.util.xml.Xpp3Dom;
30  
31  import java.io.File;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  
37  public class ReleaseUtil
38  {
39      @SuppressWarnings( "unchecked" )
40      public static Map<String, Object> getReleasePluginParameters( String workingDirectory, String pomFilename )
41          throws Exception
42      {
43          Map<String, Object> params = new HashMap<String, Object>();
44  
45          // TODO: Use the model reader so we'll can get the plugin configuration from parent too
46          Model model = getMavenModel( workingDirectory, pomFilename );
47  
48          if ( model.getBuild() != null && model.getBuild().getPlugins() != null )
49          {
50              for ( Plugin plugin : (List<Plugin>) model.getBuild().getPlugins() )
51              {
52                  if ( plugin.getGroupId() != null && plugin.getGroupId().equals( "org.apache.maven.plugins" ) &&
53                      plugin.getArtifactId() != null && plugin.getArtifactId().equals( "maven-release-plugin" ) )
54                  {
55                      Xpp3Dom dom = (Xpp3Dom) plugin.getConfiguration();
56  
57                      // TODO: use constants
58                      if ( dom != null )
59                      {
60                          Xpp3Dom configuration = dom.getChild( "releaseLabel" );
61                          if ( configuration != null )
62                          {
63                              params.put( "scm-tag", configuration.getValue() );
64                          }
65  
66                          configuration = dom.getChild( "tag" );
67                          if ( configuration != null )
68                          {
69                              params.put( "scm-tag", configuration.getValue() );
70                          }
71  
72                          configuration = dom.getChild( "tagBase" );
73                          if ( configuration != null )
74                          {
75                              params.put( "scm-tagbase", configuration.getValue() );
76                          }
77  
78                          configuration = dom.getChild( "preparationGoals" );
79                          if ( configuration != null )
80                          {
81                              params.put( "preparation-goals", configuration.getValue() );
82                          }
83  
84                          configuration = dom.getChild( "arguments" );
85                          if ( configuration != null )
86                          {
87                              params.put( "arguments", configuration.getValue() );
88                          }
89  
90                          configuration = dom.getChild( "scmCommentPrefix" );
91                          if ( configuration != null )
92                          {
93                              params.put( "scm-comment-prefix", configuration.getValue() );
94                          }
95  
96                          configuration = dom.getChild( "autoVersionSubmodules" );
97                          if ( configuration != null )
98                          {
99                              params.put( "auto-version-submodules", Boolean.valueOf( configuration.getValue() ) );
100                         }
101 
102                         configuration = dom.getChild( "addSchema" );
103                         if ( configuration != null )
104                         {
105                             params.put( "add-schema", Boolean.valueOf( configuration.getValue() ) );
106                         }
107 
108                         configuration = dom.getChild( "useReleaseProfile" );
109                         if ( configuration != null )
110                         {
111                             params.put( "use-release-profile", Boolean.valueOf( configuration.getValue() ) );
112                         }
113 
114                         configuration = dom.getChild( "goals" );
115                         if ( configuration != null )
116                         {
117                             String goals = configuration.getValue();
118                             if ( model.getDistributionManagement() != null &&
119                                 model.getDistributionManagement().getSite() != null )
120                             {
121                                 goals += " site-deploy";
122                             }
123 
124                             params.put( "perform-goals", goals );
125                         }
126                     }
127                 }
128             }
129         }
130         return params;
131     }
132 
133     public static void processProject( String workingDirectory, String pomFilename, boolean autoVersionSubmodules,
134                                        List<Map<String, String>> projects )
135         throws Exception
136     {
137         Model model = getMavenModel( workingDirectory, pomFilename );
138 
139         if ( model.getGroupId() == null )
140         {
141             model.setGroupId( model.getParent().getGroupId() );
142         }
143 
144         if ( model.getVersion() == null )
145         {
146             model.setVersion( model.getParent().getVersion() );
147         }
148 
149         setProperties( model, autoVersionSubmodules, projects );
150 
151         for ( Iterator modules = model.getModules().iterator(); modules.hasNext(); )
152         {
153             String module = StringUtils.replace( modules.next().toString(), '\\', '/' );
154 
155             processProject( workingDirectory + "/" + module, "pom.xml", autoVersionSubmodules, projects );
156         }
157     }
158 
159     private static void setProperties( Model model, boolean autoVersionSubmodules, List<Map<String, String>> projects )
160         throws Exception
161     {
162         Map<String, String> params = new HashMap<String, String>();
163 
164         params.put( "key", model.getGroupId() + ":" + model.getArtifactId() );
165 
166         if ( model.getName() == null )
167         {
168             model.setName( model.getArtifactId() );
169         }
170         params.put( "name", model.getName() );
171 
172         if ( !autoVersionSubmodules || projects.size() == 0 )
173         {
174             VersionInfo version = new DefaultVersionInfo( model.getVersion() );
175             params.put( "release", version.getReleaseVersionString() );
176             params.put( "dev", version.getNextVersion().getSnapshotVersionString() );
177         }
178         else
179         {
180             Map<String, String> rootParams = projects.get( 0 ); // get the root map
181             params.put( "release", rootParams.get( "release" ) );
182             params.put( "dev", rootParams.get( "dev" ) );
183         }
184 
185         projects.add( params );
186     }
187 
188     private static Model getMavenModel( String workingDirectory, String pomFilename )
189         throws Exception
190     {
191         MavenXpp3Reader pomReader = new MavenXpp3Reader();
192         return pomReader.read( ReaderFactory.newXmlReader( new File( workingDirectory, pomFilename ) ) );
193     }
194 }