1 package org.codehaus.plexus.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
41
42
43 public abstract class AbstractMergeMojo
44 extends AbstractMojo
45 {
46
47
48
49
50 protected abstract List getResources();
51
52 protected abstract File getOutput();
53
54
55
56
57
58
59
60
61 private File[] descriptors;
62
63
64
65
66
67
68
69
70 private ComponentDescriptorCreator cdc;
71
72
73
74
75
76 public void execute()
77 throws MojoExecutionException
78 {
79
80
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
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 }