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.registry;
21  
22  import org.apache.maven.archetype.common.ArchetypeRegistryManager;
23  import org.apache.maven.archetype.registry.ArchetypeRegistry;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import java.io.File;
30  import java.io.FileNotFoundException;
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * Adds one or more filtered extensions in the registry.
38   * The registered filtered extensions are used to discriminate
39   * text files from binary files based on their file extension
40   * during create-from-project.
41   *
42   * @author rafale
43   * @requiresProject false
44   * (@)goal add-extensions
45   * @deprecated 
46   */
47  public class AddExtensionsMojo
48      extends AbstractMojo
49  {
50      /** @component */
51      ArchetypeRegistryManager archetypeRegistryManager;
52  
53      /**
54       * The filtered extension to add to the registry.
55       * <p/>
56       * This option is mutually exclusive with extensions.
57       *
58       * @parameter expression="${extension}"
59       */
60      String extension;
61  
62      /**
63       * The filtered extensions to add to the registry: ext1,ext2,...
64       * <p/>
65       * This option is mutually exclusive with extension.
66       *
67       * @parameter expression="${extensions}"
68       */
69      String extensions;
70  
71      /**
72       * The location of the registry file.
73       *
74       * @parameter expression="${user.home}/.m2/archetype.xml"
75       */
76      private File archetypeRegistryFile;
77  
78      public void execute()
79          throws
80          MojoExecutionException,
81          MojoFailureException
82      {
83          if ( StringUtils.isEmpty( extension ) && StringUtils.isEmpty( extensions ) )
84          {
85              throw new MojoFailureException( "-Dextension or -Dextensions must be set" );
86          }
87          else if ( StringUtils.isNotEmpty( extension ) && StringUtils.isNotEmpty( extensions ) )
88          {
89              throw new MojoFailureException( "Only one of -Dextension or -Dextensions can be set" );
90          }
91  
92          try
93          {
94              List extensionsToAdd = new ArrayList();
95              if ( StringUtils.isNotEmpty( extension ) )
96              {
97                  extensionsToAdd.add( extension );
98              }
99              else
100             {
101                 extensionsToAdd.addAll( Arrays.asList( StringUtils.split( extensions, "," ) ) );
102             }
103 
104             ArchetypeRegistry registry;
105             try
106             {
107                 registry = archetypeRegistryManager.readArchetypeRegistry( archetypeRegistryFile );
108             }
109             catch ( FileNotFoundException ex )
110             {
111                 registry = archetypeRegistryManager.getDefaultArchetypeRegistry();
112             }
113 
114             Iterator extensionsToAddIterator = extensionsToAdd.iterator();
115             while ( extensionsToAddIterator.hasNext() )
116             {
117                 String extensionToAdd = (String) extensionsToAddIterator.next();
118                 if ( registry.getFilteredExtensions().contains( extensionToAdd ) )
119                 {
120                     getLog().debug( "Extension " + extensionToAdd + " already exists" );
121                 }
122                 else
123                 {
124                     registry.addFilteredExtension( extensionToAdd.trim() );
125                     getLog().debug( "Extension " + extensionToAdd + " added" );
126                 }
127             }
128             archetypeRegistryManager.writeArchetypeRegistry( archetypeRegistryFile, registry );
129         }
130         catch ( Exception ex )
131         {
132             throw new MojoExecutionException( ex.getMessage(), ex );
133         }
134     }
135 }