1 package org.codehaus.plexus.maven.plugin;
2
3 /*
4 * Copyright (c) 2004-2006, Codehaus.org
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import org.apache.maven.model.Resource;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.codehaus.plexus.cdc.ComponentDescriptorCreator;
36 import org.codehaus.plexus.cdc.ComponentDescriptorCreatorException;
37 import org.codehaus.plexus.util.FileUtils;
38
39 /**
40 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
41 * @version $Id$
42 */
43 public abstract class AbstractMergeMojo
44 extends AbstractMojo
45 {
46 // -----------------------------------------------------------------------
47 // Abstract Methods
48 // -----------------------------------------------------------------------
49
50 protected abstract List getResources();
51
52 protected abstract File getOutput();
53
54 // ----------------------------------------------------------------------
55 // Parameters
56 // ----------------------------------------------------------------------
57
58 /**
59 * @parameter
60 */
61 private File[] descriptors;
62
63 // ----------------------------------------------------------------------
64 // Components
65 // ----------------------------------------------------------------------
66
67 /**
68 * @component
69 */
70 private ComponentDescriptorCreator cdc;
71
72 // ----------------------------------------------------------------------
73 //
74 // ----------------------------------------------------------------------
75
76 public void execute()
77 throws MojoExecutionException
78 {
79 // ----------------------------------------------------------------------
80 // Locate files
81 // ----------------------------------------------------------------------
82
83 List files = new ArrayList();
84
85 for ( Iterator it = getResources().iterator(); it.hasNext(); )
86 {
87 Resource resource = (Resource) it.next();
88
89 String includes = "META-INF/plexus/components.xml";
90
91 String excludes = "";
92
93 for ( Iterator j = resource.getExcludes().iterator(); j.hasNext(); )
94 {
95 String exclude = (String) j.next();
96 excludes += exclude + ",";
97 }
98
99 try
100 {
101 File basedir = new File( resource.getDirectory() );
102
103 getLog().debug( "Searching for component.xml files. Basedir: " + basedir.getAbsolutePath() +
104 ", includes: " + includes + ", excludes=" + excludes );
105
106 if ( !basedir.isDirectory() )
107 {
108 getLog().debug( "Skipping, not a directory." );
109
110 continue;
111 }
112
113 List list = FileUtils.getFiles( basedir, includes, excludes );
114
115 files.addAll( list );
116 }
117 catch ( IOException e )
118 {
119 throw new MojoExecutionException( "Error while scanning for component.xml files.", e );
120 }
121 }
122
123 if ( descriptors != null )
124 {
125 files.addAll( Arrays.asList( descriptors ) );
126 }
127
128 // ----------------------------------------------------------------------
129 // Merge the component set descriptors
130 // ----------------------------------------------------------------------
131
132 if ( files.isEmpty() )
133 {
134 getLog().debug( "Didn't find any files to merge." );
135
136 return;
137 }
138
139 getLog().debug( "Found " + files.size() + " files to merge:" );
140
141 for ( Iterator it = files.iterator(); it.hasNext(); )
142 {
143 File file = (File) it.next();
144
145 getLog().debug( file.getAbsolutePath() );
146 }
147
148 try
149 {
150 cdc.mergeDescriptors( getOutput(), files );
151 }
152 catch ( ComponentDescriptorCreatorException e )
153 {
154 throw new MojoExecutionException( "Error while executing component descritor creator.", e );
155 }
156 }
157 }