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  
34  
35  /**
36   * Updates the local catalog
37   *
38   * @phase install
39   * @goal update-local-catalog
40   *
41   * @author rafale
42   */
43  public class UpdateLocalCatalogMojo
44      extends AbstractMojo
45      implements ContextEnabled
46  {
47      /** @component */
48      private org.apache.maven.archetype.Archetype archetyper;
49      
50      /** @component role="org.apache.maven.archetype.source.ArchetypeDataSource" */
51      private Map archetypeSources;
52  
53      /**
54       * The project artifact, which should have the LATEST metadata added to it.
55       *
56       * @parameter expression="${project}"
57       * @required
58       * @readonly
59       */
60      private MavenProject project;
61  
62      /**
63       * The project artifact, which should have the LATEST metadata added to it.
64       *
65       * @parameter expression="${settings.localRepository}"
66       * @required
67       * @readonly
68       */
69      private File localRepository;
70  
71      /**
72       * The Maven settings.
73       *
74       * @parameter expression="${settings}"
75       * @required
76       * @readonly
77       */
78      private Settings settings;
79  
80      public void execute( )
81          throws MojoExecutionException
82      {
83          Archetype archetype = new Archetype(  );
84          archetype.setGroupId( project.getGroupId(  ) );
85          archetype.setArtifactId( project.getArtifactId(  ) );
86          archetype.setVersion( project.getVersion(  ) );
87          archetype.setDescription( project.getName(  ) );
88  //        archetype.setRepository( localRepository.toString(  ) );
89  //            archetype.setGoals(project.get);
90  //            archetype.setProperties(project.get);
91          
92          archetyper.updateLocalCatalog(archetype);
93          /*
94          File archetypeCatalogPropertiesFile = new File( System.getProperty( "user.home" ), ".m2/archetype-catalog.properties" );
95  
96          if ( archetypeCatalogPropertiesFile.exists(  ) )
97          {
98              Properties archetypeCatalogProperties = PropertyUtils.loadProperties( archetypeCatalogPropertiesFile );
99  
100             getLog(  ).debug( "Updating catalogs " + archetypeCatalogProperties );
101 
102             String[] sources = StringUtils.split( archetypeCatalogProperties.getProperty( "sources" ), "," );
103 
104             for ( int i = 0; i < sources.length; i++ )
105             {
106                 String sourceRoleHint = sources[i].trim();
107 
108                 try
109                 {
110                     getLog(  ).debug( "Updating catalog " + sourceRoleHint );
111 
112                     ArchetypeDataSource source = (ArchetypeDataSource) archetypeSources.get( sourceRoleHint );
113 
114                     source.updateCatalog( getArchetypeSourceProperties( sourceRoleHint, archetypeCatalogProperties ), archetype, settings );
115 
116                     getLog(  ).
117                         info( "Updated " + sourceRoleHint + " using repository " + localRepository.toString(  ) );
118                 }
119                 catch ( ArchetypeDataSourceException ex )
120                 {
121                     getLog(  ).
122                         warn( "Can't update " + sourceRoleHint + " using repository " + localRepository.toString(  ) );
123                 }
124             }
125         }
126         else
127         {
128             getLog(  ).debug( "Not updating wiki catalog" );
129         }
130         */
131     }
132 
133     private Properties getArchetypeSourceProperties( String sourceRoleHint, Properties archetypeCatalogProperties )
134     {
135         Properties p = new Properties(  );
136 
137         for ( Iterator i = archetypeCatalogProperties.keySet(  ).iterator(  ); i.hasNext(  ); )
138         {
139             String key = (String) i.next();
140 
141             if ( key.startsWith( sourceRoleHint ) )
142             {
143                 String k = key.substring( sourceRoleHint.length(  ) + 1 );
144 
145                 p.setProperty( k, archetypeCatalogProperties.getProperty( key ) );
146             }
147         }
148 
149         return p;
150     }
151 }