1 package org.andromda.maven.plugin.cartridge;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8 import org.apache.commons.io.FilenameUtils;
9 import org.apache.maven.artifact.Artifact;
10 import org.apache.maven.model.Dependency;
11 import org.apache.maven.plugin.AbstractMojo;
12 import org.apache.maven.plugin.MojoExecutionException;
13 import org.apache.maven.project.MavenProject;
14 import org.apache.maven.settings.Settings;
15 import org.codehaus.plexus.archiver.ArchiverException;
16 import org.codehaus.plexus.archiver.UnArchiver;
17 import org.codehaus.plexus.archiver.manager.ArchiverManager;
18
19
20
21
22
23
24
25
26 public abstract class AbstractCartridgeTestMojo
27 extends AbstractMojo
28 {
29
30
31
32
33
34 protected File reportDirectory;
35
36
37
38
39
40
41
42 protected File actualDirectory;
43
44
45
46
47
48
49 protected File expectedDirectory;
50
51
52
53
54
55
56 protected File expectedOutputArchive;
57
58
59
60
61
62
63
64 protected String configurationUri;
65
66
67
68
69
70
71 protected MavenProject project;
72
73
74
75
76 protected List<String> propertyFiles;
77
78
79
80
81
82
83
84
85
86 protected Settings settings;
87
88
89
90
91
92
93
94 protected String binaryOutputSuffixes;
95
96
97
98
99
100
101
102 protected ArchiverManager archiverManager;
103
104
105
106
107
108
109 protected boolean skip;
110
111
112
113
114
115
116 protected boolean skipTests;
117
118
119
120
121
122
123 protected boolean testFailureIgnore;
124
125
126
127
128
129
130 protected boolean lastModifiedCheck;
131
132
133
134
135
136 protected void changeScopeForTestDependencies()
137 {
138
139
140 for (final Dependency dependency : (Iterable<Dependency>) this.project.getTestDependencies())
141 {
142
143 if (dependency.getScope().equals(Artifact.SCOPE_TEST))
144 {
145 dependency.setScope(Artifact.SCOPE_RUNTIME);
146 project.getDependencies().add(dependency);
147 }
148 }
149
150 final Set artifacts = new HashSet<Artifact>(this.project.getArtifacts());
151 for (final Artifact artifact : (Iterable<Artifact>) this.project.getTestArtifacts())
152 {
153
154 if (artifact.getScope().equals(Artifact.SCOPE_TEST))
155 {
156 artifact.setScope(Artifact.SCOPE_RUNTIME);
157 artifacts.add(artifact);
158 }
159 }
160 project.setArtifacts(artifacts);
161 }
162
163
164
165
166
167
168
169
170 protected void unpack(
171 final File file,
172 final File location)
173 throws MojoExecutionException
174 {
175 final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase();
176 try
177 {
178 final UnArchiver unArchiver;
179 unArchiver = this.archiverManager.getUnArchiver(archiveExt);
180 unArchiver.setSourceFile(file);
181 location.mkdirs();
182 unArchiver.setDestDirectory(location);
183 unArchiver.extract();
184 }
185 catch (Throwable throwable)
186 {
187 if (this.testFailureIgnore)
188 {
189 this.getLog().error(this.project.getArtifactId() + ": Error unpacking file: " + file + " to " + location, throwable);
190 }
191 else if (throwable instanceof IOException || throwable instanceof ArchiverException)
192 {
193 throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location, throwable);
194 }
195 }
196 }
197 }