View Javadoc
1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.io.OutputStream;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  /**
35   * Dumps the role hints of the available repository layouts to a properties file.
36   *
37   * @author Benjamin Bentmann
38   * @goal dump-repo-layouts
39   * @phase validate
40   */
41  public class DumpRepoLayoutsMojo
42      extends AbstractMojo
43  {
44  
45      /**
46       * Project base directory used for manual path alignment.
47       *
48       * @parameter default-value="${basedir}"
49       * @readonly
50       */
51      private File basedir;
52  
53      /**
54       * The available repository layouts, as a map.
55       *
56       * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
57       */
58      private Map repositoryLayouts;
59  
60      /**
61       * The available repository layouts, as a list.
62       *
63       * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
64       */
65      private List repoLayouts;
66  
67      /**
68       * The path to the properties file used to dump the repository layouts.
69       *
70       * @parameter property="collections.layoutsFile"
71       */
72      private File layoutsFile;
73  
74      /**
75       * Runs this mojo.
76       *
77       * @throws MojoFailureException If the output file could not be created.
78       */
79      public void execute()
80          throws MojoExecutionException, MojoFailureException
81      {
82          Properties layoutProperties = new Properties();
83  
84          getLog().info( "[MAVEN-CORE-IT-LOG] Dumping repository layouts" );
85  
86          layoutProperties.setProperty( "layouts", Integer.toString( repositoryLayouts.size() ) );
87  
88          for ( Object o : repositoryLayouts.keySet() )
89          {
90              String roleHint = (String) o;
91              Object repoLayout = repositoryLayouts.get( roleHint );
92              if ( repoLayout != null )
93              {
94                  layoutProperties.setProperty( "layouts." + roleHint, repoLayout.getClass().getName() );
95              }
96              else
97              {
98                  layoutProperties.setProperty( "layouts." + roleHint, "" );
99              }
100             getLog().info( "[MAVEN-CORE-IT-LOG]   " + roleHint + " = " + repoLayout );
101         }
102 
103         if ( repoLayouts.size() != repositoryLayouts.size() )
104         {
105             throw new MojoExecutionException( "Inconsistent collection: " + repoLayouts + " vs " + repositoryLayouts );
106         }
107 
108         if ( !layoutsFile.isAbsolute() )
109         {
110             layoutsFile = new File( basedir, layoutsFile.getPath() );
111         }
112 
113         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + layoutsFile );
114 
115         OutputStream out = null;
116         try
117         {
118             layoutsFile.getParentFile().mkdirs();
119             out = new FileOutputStream( layoutsFile );
120             layoutProperties.store( out, "MAVEN-CORE-IT-LOG" );
121         }
122         catch ( IOException e )
123         {
124             throw new MojoExecutionException( "Output file could not be created: " + layoutsFile, e );
125         }
126         finally
127         {
128             if ( out != null )
129             {
130                 try
131                 {
132                     out.close();
133                 }
134                 catch ( IOException e )
135                 {
136                     // just ignore
137                 }
138             }
139         }
140 
141         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + layoutsFile );
142     }
143 
144 }