1 package org.andromda.maven.plugin.modelarchiver;
2
3 import java.io.File;
4 import org.apache.commons.io.FileUtils;
5 import org.apache.commons.io.FilenameUtils;
6 import org.apache.maven.plugin.MojoExecutionException;
7
8
9
10
11
12
13
14
15
16
17 public class XmlArchiverMojo
18 extends BaseArchiveMojo
19 {
20 private static final String ARTIFACT_TYPE = "xml";
21
22
23
24
25
26
27
28
29 private String modelFilePattern;
30
31
32
33
34
35
36
37
38 public void execute()
39 throws MojoExecutionException
40 {
41 if (getLog().isDebugEnabled())
42 {
43 getLog().debug(" ======= XmlArchiverMojo settings =======");
44 getLog().debug("modelSourceDirectory[" + modelSourceDirectory + ']');
45 getLog().debug("workDirectory[" + workDirectory + ']');
46 getLog().debug("outputDirectory[" + outputDirectory + ']');
47 getLog().debug("finalName[" + finalName + ']');
48 }
49
50 try
51 {
52 final File buildDirectory = this.workDirectory;
53 if (buildDirectory.exists())
54 {
55 deleteFiles(buildDirectory.getAbsolutePath(), ARTIFACT_TYPE);
56 }
57 else
58 {
59 buildDirectory.mkdirs();
60 }
61
62
63
64 if (modelSourceDirectory.exists())
65 {
66 final File[] modelFiles = modelSourceDirectory.listFiles();
67 for (final File file : modelFiles)
68 {
69 if (file.isFile() && file.toString().matches(this.modelFilePattern))
70 {
71 final File newFile =
72 new File(buildDirectory,
73 this.finalName + '.' + FilenameUtils.getExtension(file.getName()));
74 getLog().info("File " + file + " copied to " + newFile);
75 FileUtils.copyFile(file, newFile);
76
77 String contents = FileUtils.readFileToString(newFile);
78 final String version = escapePattern(this.project.getVersion());
79
80
81
82
83
84
85
86
87
88
89
90 contents =
91 contents.replaceAll(
92 "_Profile\\-" + version,
93 "_Profile");
94
95
96 FileUtils.writeStringToFile(newFile, contents);
97
98 setArtifactFile(newFile);
99 }
100 }
101 }
102 }
103 catch (final Throwable throwable)
104 {
105 throw new MojoExecutionException("Error assembling model", throwable);
106 }
107 }
108 }