1 | |
package org.jbehave.mojo; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.util.Arrays; |
5 | |
import java.util.HashSet; |
6 | |
import java.util.Set; |
7 | |
|
8 | |
import org.apache.commons.collections.CollectionUtils; |
9 | |
import org.apache.commons.collections.Predicate; |
10 | |
import org.apache.maven.artifact.Artifact; |
11 | |
import org.apache.maven.plugin.MojoExecutionException; |
12 | |
import org.apache.maven.project.MavenProject; |
13 | |
import org.codehaus.plexus.archiver.UnArchiver; |
14 | |
import org.codehaus.plexus.archiver.manager.ArchiverManager; |
15 | |
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector; |
16 | |
import org.codehaus.plexus.util.StringUtils; |
17 | |
import org.jbehave.core.reporters.StoryReporterBuilder; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class UnpackViewResources extends AbstractEmbedderMojo { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
MavenProject project; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
ArchiverManager archiverManager; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | String[] resourceArtifactIds = new String[] { "jbehave-site-resources", "jbehave-core" }; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | String[] resourceTypes = new String[] { "zip" }; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
String resourceIncludes; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
String resourcesExcludes; |
59 | |
|
60 | |
public void execute() throws MojoExecutionException { |
61 | 0 | File destination = viewDirectory(); |
62 | 0 | for (Artifact artifact : resourceArtifacts()) { |
63 | 0 | unpack(artifact.getFile(), destination, resourceIncludes, resourcesExcludes); |
64 | |
} |
65 | 0 | } |
66 | |
|
67 | |
private File viewDirectory() { |
68 | 0 | StoryReporterBuilder storyReporterBuilder = newEmbedder().configuration().storyReporterBuilder(); |
69 | 0 | String build = project.getBuild().getDirectory(); |
70 | 0 | String output = storyReporterBuilder.outputDirectory().getName(); |
71 | 0 | String view = storyReporterBuilder.viewResources().getProperty("viewDirectory"); |
72 | 0 | return new File(build + "/" + output + "/" + view); |
73 | |
} |
74 | |
|
75 | |
private Set<Artifact> resourceArtifacts() { |
76 | 0 | Set<Artifact> artifacts = allArtifacts(); |
77 | 0 | CollectionUtils.filter(artifacts, new Predicate() { |
78 | |
public boolean evaluate(Object object) { |
79 | 0 | Artifact artifact = (Artifact) object; |
80 | 0 | return allowedBy("artifactId", artifact.getArtifactId(), resourceArtifactIds) |
81 | |
&& allowedBy("type", artifact.getType(), resourceTypes); |
82 | |
} |
83 | |
}); |
84 | 0 | return artifacts; |
85 | |
} |
86 | |
|
87 | |
private boolean allowedBy(String name, String property, String[] values) { |
88 | 0 | boolean allowed = false; |
89 | 0 | if (values.length > 0) { |
90 | 0 | for (String value : values) { |
91 | 0 | if (property.equals(value)) { |
92 | 0 | allowed = true; |
93 | 0 | break; |
94 | |
} |
95 | |
} |
96 | |
} else { |
97 | 0 | allowed = true; |
98 | |
} |
99 | 0 | if (!allowed) { |
100 | 0 | getLog().debug("Artifact property " + name + " not allowed by values " + Arrays.asList(values)); |
101 | |
} |
102 | 0 | return allowed; |
103 | |
} |
104 | |
|
105 | |
@SuppressWarnings("unchecked") |
106 | |
private Set<Artifact> allArtifacts() { |
107 | 0 | return new HashSet<Artifact>(project.getArtifacts()); |
108 | |
} |
109 | |
|
110 | |
private void unpack(File file, File destination, String includes, String excludes) throws MojoExecutionException { |
111 | |
try { |
112 | 0 | destination.mkdirs(); |
113 | |
|
114 | 0 | UnArchiver unArchiver = archiverManager.getUnArchiver(file); |
115 | 0 | unArchiver.setSourceFile(file); |
116 | 0 | unArchiver.setDestDirectory(destination); |
117 | |
|
118 | 0 | if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) { |
119 | 0 | IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() }; |
120 | 0 | if (StringUtils.isNotEmpty(excludes)) { |
121 | 0 | selectors[0].setExcludes(excludes.split(",")); |
122 | |
} |
123 | 0 | if (StringUtils.isNotEmpty(includes)) { |
124 | 0 | selectors[0].setIncludes(includes.split(",")); |
125 | |
} |
126 | 0 | unArchiver.setFileSelectors(selectors); |
127 | |
} |
128 | |
|
129 | 0 | unArchiver.extract(); |
130 | |
|
131 | 0 | getLog().info("Unpacked " + file + " to " + destination); |
132 | 0 | } catch (Exception e) { |
133 | 0 | throw new MojoExecutionException("Failed unpacking " + file + " to " + destination, e); |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
} |