1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.war.packaging;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugins.war.Overlay;
30 import org.codehaus.plexus.interpolation.InterpolationException;
31
32
33
34
35
36
37 public class ArtifactsPackagingTask extends AbstractWarPackagingTask {
38
39
40
41
42 public static final String TLD_PATH = "WEB-INF/tld/";
43
44
45
46
47 public static final String SERVICES_PATH = "WEB-INF/services/";
48
49
50
51
52 public static final String MODULES_PATH = "WEB-INF/modules/";
53
54
55
56
57 public static final String EXTENSIONS_PATH = "WEB-INF/extensions/";
58
59 private final Set<Artifact> artifacts;
60
61 private final String id;
62
63
64
65
66
67 public ArtifactsPackagingTask(Set<Artifact> artifacts, Overlay currentProjectOverlay) {
68 this.artifacts = artifacts;
69 this.id = currentProjectOverlay.getId();
70 }
71
72 @Override
73 public void performPackaging(WarPackagingContext context) throws MojoExecutionException {
74 try {
75 final ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
76 final List<String> duplicates = findDuplicates(context, artifacts);
77
78 for (Artifact artifact : artifacts) {
79 String targetFileName = getArtifactFinalName(context, artifact);
80
81 context.getLog().debug("Processing: " + targetFileName);
82
83 if (duplicates.contains(targetFileName)) {
84 context.getLog().debug("Duplicate found: " + targetFileName);
85 targetFileName = artifact.getGroupId() + "-" + targetFileName;
86 context.getLog().debug("Renamed to: " + targetFileName);
87 }
88 context.getWebappStructure().registerTargetFileName(artifact, targetFileName);
89
90 if (!artifact.isOptional() && filter.include(artifact)) {
91 try {
92 String type = artifact.getType();
93 if ("tld".equals(type)) {
94 copyFile(id, context, artifact.getFile(), TLD_PATH + targetFileName);
95 } else if ("aar".equals(type)) {
96 copyFile(id, context, artifact.getFile(), SERVICES_PATH + targetFileName);
97 } else if ("mar".equals(type)) {
98 copyFile(id, context, artifact.getFile(), MODULES_PATH + targetFileName);
99 } else if ("xar".equals(type)) {
100 copyFile(id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName);
101 } else if ("jar".equals(type)
102 || "ejb".equals(type)
103 || "ejb-client".equals(type)
104 || "test-jar".equals(type)
105 || "bundle".equals(type)) {
106 copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
107 } else if ("par".equals(type)) {
108 targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
109 copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
110 } else if ("war".equals(type)) {
111
112 context.getLog()
113 .debug("war artifacts are handled as overlays, ignoring [" + artifact + "]");
114 } else if ("zip".equals(type)) {
115
116 context.getLog()
117 .debug("zip artifacts are handled as overlays, ignoring [" + artifact + "]");
118 } else {
119 context.getLog()
120 .debug("Artifact of type [" + type + "] is not supported, ignoring [" + artifact
121 + "]");
122 }
123 } catch (IOException e) {
124 throw new MojoExecutionException("Failed to copy file for artifact [" + artifact + "]", e);
125 }
126 }
127 }
128 } catch (InterpolationException e) {
129 throw new MojoExecutionException(e.getMessage(), e);
130 }
131 }
132
133
134
135
136
137
138
139
140 private List<String> findDuplicates(WarPackagingContext context, Set<Artifact> artifacts)
141 throws InterpolationException {
142 List<String> duplicates = new ArrayList<>();
143 List<String> identifiers = new ArrayList<>();
144 for (Artifact artifact : artifacts) {
145 String candidate = getArtifactFinalName(context, artifact);
146 if (identifiers.contains(candidate)) {
147 duplicates.add(candidate);
148 } else {
149 identifiers.add(candidate);
150 }
151 }
152 return duplicates;
153 }
154 }