View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.maven.archetype.mojos;
21  
22  import org.apache.maven.archetype.catalog.Archetype;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.ContextEnabled;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.settings.Settings;
28  
29  import java.io.File;
30  import java.util.Iterator;
31  import java.util.Map;
32  import java.util.Properties;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  
36  /**
37   * Updates the local catalog
38   *
39   * @phase install
40   * @goal update-local-catalog
41   *
42   * @author rafale
43   */
44  public class UpdateLocalCatalogMojo
45      extends AbstractMojo
46      implements ContextEnabled
47  {
48      /** @component */
49      private org.apache.maven.archetype.Archetype archetyper;
50      
51      /** @component role="org.apache.maven.archetype.source.ArchetypeDataSource" */
52      private Map archetypeSources;
53  
54      /**
55       * The project artifact, which should have the LATEST metadata added to it.
56       *
57       * @parameter expression="${project}"
58       * @required
59       * @readonly
60       */
61      private MavenProject project;
62  
63      /**
64       * The project artifact, which should have the LATEST metadata added to it.
65       *
66       * @parameter expression="${settings.localRepository}"
67       * @required
68       * @readonly
69       */
70      private File localRepository;
71  
72      /**
73       * The Maven settings.
74       *
75       * @parameter expression="${settings}"
76       * @required
77       * @readonly
78       */
79      private Settings settings;
80  
81      public void execute( )
82          throws MojoExecutionException
83      {
84          Archetype archetype = new Archetype(  );
85          archetype.setGroupId( project.getGroupId(  ) );
86          archetype.setArtifactId( project.getArtifactId(  ) );
87          archetype.setVersion( project.getVersion(  ) );
88          if (StringUtils.isNotEmpty(project.getDescription()))
89          {
90              archetype.setDescription(project.getDescription());
91          }
92          else
93          {
94              archetype.setDescription(project.getName());
95          }
96  //        archetype.setRepository( localRepository.toString(  ) );
97  //            archetype.setGoals(project.get);
98  //            archetype.setProperties(project.get);
99          
100         archetyper.updateLocalCatalog(archetype);
101         /*
102         File archetypeCatalogPropertiesFile = new File( System.getProperty( "user.home" ), ".m2/archetype-catalog.properties" );
103 
104         if ( archetypeCatalogPropertiesFile.exists(  ) )
105         {
106             Properties archetypeCatalogProperties = PropertyUtils.loadProperties( archetypeCatalogPropertiesFile );
107 
108             getLog(  ).debug( "Updating catalogs " + archetypeCatalogProperties );
109 
110             String[] sources = StringUtils.split( archetypeCatalogProperties.getProperty( "sources" ), "," );
111 
112             for ( int i = 0; i < sources.length; i++ )
113             {
114                 String sourceRoleHint = sources[i].trim();
115 
116                 try
117                 {
118                     getLog(  ).debug( "Updating catalog " + sourceRoleHint );
119 
120                     ArchetypeDataSource source = (ArchetypeDataSource) archetypeSources.get( sourceRoleHint );
121 
122                     source.updateCatalog( getArchetypeSourceProperties( sourceRoleHint, archetypeCatalogProperties ), archetype, settings );
123 
124                     getLog(  ).
125                         info( "Updated " + sourceRoleHint + " using repository " + localRepository.toString(  ) );
126                 }
127                 catch ( ArchetypeDataSourceException ex )
128                 {
129                     getLog(  ).
130                         warn( "Can't update " + sourceRoleHint + " using repository " + localRepository.toString(  ) );
131                 }
132             }
133         }
134         else
135         {
136             getLog(  ).debug( "Not updating wiki catalog" );
137         }
138         */
139     }
140 
141     private Properties getArchetypeSourceProperties( String sourceRoleHint, Properties archetypeCatalogProperties )
142     {
143         Properties p = new Properties(  );
144 
145         for ( Iterator i = archetypeCatalogProperties.keySet(  ).iterator(  ); i.hasNext(  ); )
146         {
147             String key = (String) i.next();
148 
149             if ( key.startsWith( sourceRoleHint ) )
150             {
151                 String k = key.substring( sourceRoleHint.length(  ) + 1 );
152 
153                 p.setProperty( k, archetypeCatalogProperties.getProperty( key ) );
154             }
155         }
156 
157         return p;
158     }
159 }