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   * Removes one or more filtered extensions from 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 remove-extensions
45   * @deprecated 
46   */
47  public class RemoveExtensionsMojo
48      extends AbstractMojo
49  {
50      /** @component */
51      ArchetypeRegistryManager archetypeRegistryManager;
52  
53      /**
54       * The filtered extension to remove from 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 remove from 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 extensionsToRemove = new ArrayList();
95              if ( StringUtils.isNotEmpty( extension ) )
96              {
97                  extensionsToRemove.add( extension );
98              }
99              else
100             {
101                 extensionsToRemove.addAll(
102                     Arrays.asList( StringUtils.split( extensions, "," ) )
103                 );
104             }
105 
106             ArchetypeRegistry registry;
107             try
108             {
109                 registry = archetypeRegistryManager.readArchetypeRegistry( archetypeRegistryFile );
110             }
111             catch ( FileNotFoundException ex )
112             {
113                 registry = archetypeRegistryManager.getDefaultArchetypeRegistry();
114             }
115 
116             Iterator extensionsToRemoveIterator = extensionsToRemove.iterator();
117             while ( extensionsToRemoveIterator.hasNext() )
118             {
119                 String extensionToRemove = (String) extensionsToRemoveIterator.next();
120                 if ( registry.getFilteredExtensions().contains( extensionToRemove ) )
121                 {
122                     registry.removeFilteredExtension( extensionToRemove );
123                     getLog().debug( "Extension " + extensionToRemove + " removed" );
124                 }
125                 else
126                 {
127                     getLog().debug( "Extension " + extensionToRemove + " doesn't exist" );
128                 }
129             }
130             archetypeRegistryManager.writeArchetypeRegistry( archetypeRegistryFile, registry );
131         }
132         catch ( Exception ex )
133         {
134             throw new MojoExecutionException( ex.getMessage(), ex );
135         }
136     }
137 }