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 language directories in the registry.
38   * The registered language directories are used to discriminate
39   * packaging directories from unpackaged ones based on their name
40   * during create-from-project.
41   *
42   * @author rafale
43   * @requiresProject false
44   * (@)goal add-languages
45   * @deprecated 
46   */
47  public class AddLanguagesMojo
48      extends AbstractMojo
49  {
50      /** @component */
51      ArchetypeRegistryManager archetypeRegistryManager;
52  
53      /**
54       * The language directory to add to the registry.
55       * <p/>
56       * This option is mutually exclusive with language directories.
57       *
58       * @parameter expression="${language}"
59       */
60      String language;
61  
62      /**
63       * The language directories to add to the registry: lang1,lang2,...
64       * <p/>
65       * This option is mutually exclusive with language directory.
66       *
67       * @parameter expression="${languages}"
68       */
69      String languages;
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( language ) && StringUtils.isEmpty( languages ) )
84          {
85              throw new MojoFailureException( "-Dlanguage or -Dlanguages must be set" );
86          }
87          else if ( StringUtils.isNotEmpty( language ) && StringUtils.isNotEmpty( languages ) )
88          {
89              throw new MojoFailureException( "Only one of -Dlanguage or -Dlanguages can be set" );
90          }
91  
92          try
93          {
94              List languagesToAdd = new ArrayList();
95              if ( StringUtils.isNotEmpty( language ) )
96              {
97                  languagesToAdd.add( language );
98              }
99              else
100             {
101                 languagesToAdd.addAll( Arrays.asList( StringUtils.split( languages, "," ) ) );
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 languagesToAddIterator = languagesToAdd.iterator();
115             while ( languagesToAddIterator.hasNext() )
116             {
117                 String languageToAdd = (String) languagesToAddIterator.next();
118                 if ( registry.getLanguages().contains( languageToAdd ) )
119                 {
120                     getLog().debug( "Language " + languageToAdd + " already exists" );
121                 }
122                 else
123                 {
124                     registry.addLanguage( languageToAdd.trim() );
125                     getLog().debug( "Language " + languageToAdd + " added" );
126                 }
127             }
128             archetypeRegistryManager.writeArchetypeRegistry( archetypeRegistryFile, registry );
129         }
130         catch ( Exception ex )
131         {
132             throw new MojoExecutionException( ex.getMessage(), ex );
133         }
134     }
135 }