1 package org.sonatype.plugins.munge;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.List;
6
7 import org.apache.maven.model.Build;
8 import org.apache.maven.plugin.AbstractMojo;
9 import org.apache.maven.plugin.MojoExecutionException;
10 import org.apache.maven.project.MavenProject;
11 import org.codehaus.plexus.util.FileUtils;
12
13
14
15
16
17
18
19
20
21 public class MungeMojo
22 extends AbstractMojo
23 {
24
25
26
27
28
29 private String mungedDirectory;
30
31
32
33
34
35
36
37 private String symbols;
38
39
40
41
42
43
44 private String includes;
45
46
47
48
49
50
51 private String excludes;
52
53
54
55
56
57 private Build build;
58
59
60
61
62
63 private MavenProject executedProject;
64
65 @SuppressWarnings( "unchecked" )
66 public void execute()
67 throws MojoExecutionException
68 {
69 Munge.symbols.clear();
70 for ( final String s : symbols.split( "," ) )
71 {
72 Munge.symbols.put( s, Boolean.TRUE );
73 }
74
75 final String mungedMainDirectory = mungedDirectory + File.separator + "main";
76 final String mungedTestDirectory = mungedDirectory + File.separator + "test";
77
78 munge( build.getSourceDirectory(), mungedMainDirectory, includes, excludes );
79 munge( build.getTestSourceDirectory(), mungedTestDirectory, includes, excludes );
80
81 if ( null != executedProject )
82 {
83 executedProject.getCompileSourceRoots().clear();
84 executedProject.addCompileSourceRoot( mungedMainDirectory );
85
86 executedProject.getTestCompileSourceRoots().clear();
87 executedProject.addTestCompileSourceRoot( mungedTestDirectory );
88
89 final Build executedBuild = executedProject.getBuild();
90 executedBuild.setDirectory( mungedDirectory );
91
92 executedBuild.setOutputDirectory( mungedDirectory + File.separator + "classes" );
93 executedBuild.setTestOutputDirectory( mungedDirectory + File.separator + "test-classes" );
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106 @SuppressWarnings( "unchecked" )
107 public static void munge( final String from, final String to, final String includes, final String excludes )
108 throws MojoExecutionException
109 {
110 try
111 {
112 for ( final File f : (List<File>) FileUtils.getFiles( new File( from ), includes, excludes ) )
113 {
114 final String inPath = f.getPath();
115 final String outPath = inPath.replace( from, to );
116 new File( outPath ).getParentFile().mkdirs();
117 final Munge munge = new Munge( inPath, outPath );
118 munge.process();
119 munge.close();
120 }
121 }
122 catch ( final IOException e )
123 {
124 throw new MojoExecutionException( e.toString() );
125 }
126 }
127 }