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  
89          if ( StringUtils.isNotEmpty( project.getDescription() ) )
90          {
91              archetype.setDescription( project.getDescription() );
92          }
93          else
94          {
95              archetype.setDescription( project.getName() );
96          }
97  
98  //        archetype.setRepository( localRepository.toString() );
99  //            archetype.setGoals( project.get );
100 //            archetype.setProperties( project.get );
101 
102         archetyper.updateLocalCatalog( archetype );
103         /*
104         File archetypeCatalogPropertiesFile = new File( System.getProperty( "user.home" ), ".m2/archetype-catalog.properties" );
105 
106         if ( archetypeCatalogPropertiesFile.exists() )
107         {
108             Properties archetypeCatalogProperties = PropertyUtils.loadProperties( archetypeCatalogPropertiesFile );
109 
110             getLog(  ).debug( "Updating catalogs " + archetypeCatalogProperties );
111 
112             String[] sources = StringUtils.split( archetypeCatalogProperties.getProperty( "sources" ), "," );
113 
114             for ( int i = 0; i < sources.length; i++ )
115             {
116                 String sourceRoleHint = sources[i].trim();
117 
118                 try
119                 {
120                     getLog().debug( "Updating catalog " + sourceRoleHint );
121 
122                     ArchetypeDataSource source = (ArchetypeDataSource) archetypeSources.get( sourceRoleHint );
123 
124                     source.updateCatalog( getArchetypeSourceProperties( sourceRoleHint, archetypeCatalogProperties ), archetype, settings );
125 
126                     getLog().
127                         info( "Updated " + sourceRoleHint + " using repository " + localRepository.toString() );
128                 }
129                 catch ( ArchetypeDataSourceException ex )
130                 {
131                     getLog().
132                         warn( "Can't update " + sourceRoleHint + " using repository " + localRepository.toString() );
133                 }
134             }
135         }
136         else
137         {
138             getLog(  ).debug( "Not updating wiki catalog" );
139         }
140         */
141     }
142 
143     private Properties getArchetypeSourceProperties( String sourceRoleHint, Properties archetypeCatalogProperties )
144     {
145         Properties p = new Properties();
146 
147         for ( Iterator i = archetypeCatalogProperties.keySet().iterator(); i.hasNext(); )
148         {
149             String key = (String) i.next();
150 
151             if ( key.startsWith( sourceRoleHint ) )
152             {
153                 String k = key.substring( sourceRoleHint.length() + 1 );
154 
155                 p.setProperty( k, archetypeCatalogProperties.getProperty( key ) );
156             }
157         }
158 
159         return p;
160     }
161 }