1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.utils;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.logging.Log;
29 import org.codehaus.plexus.archiver.ArchiverException;
30 import org.codehaus.plexus.archiver.UnArchiver;
31 import org.codehaus.plexus.archiver.manager.ArchiverManager;
32 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
33 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
34 import org.codehaus.plexus.components.io.filemappers.FileMapper;
35 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
36 import org.sonatype.plexus.build.incremental.BuildContext;
37
38
39
40
41
42 @Named
43 @Singleton
44 public class UnpackUtil {
45
46
47
48
49 private final ArchiverManager archiverManager;
50
51
52
53
54 private final BuildContext buildContext;
55
56
57
58
59
60
61
62 @Inject
63 public UnpackUtil(ArchiverManager archiverManager, BuildContext buildContext) {
64 this.archiverManager = archiverManager;
65 this.buildContext = buildContext;
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public void unpack(
83 File file,
84 String type,
85 File location,
86 String includes,
87 String excludes,
88 String encoding,
89 boolean ignorePermissions,
90 FileMapper[] fileMappers,
91 Log logger)
92 throws MojoExecutionException {
93 try {
94 logUnpack(logger, file, location, includes, excludes);
95
96 location.mkdirs();
97 if (!location.exists()) {
98 throw new MojoExecutionException(
99 "Location to write unpacked files to could not be created: " + location);
100 }
101
102 if (file.isDirectory()) {
103
104 throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, "
105 + "unpack should be executed after packaging: see MDEP-98.");
106 }
107
108 UnArchiver unArchiver;
109
110 try {
111 unArchiver = archiverManager.getUnArchiver(type);
112 logger.debug("Found unArchiver: " + unArchiver.getClass().getName() + " by type: " + type);
113 } catch (NoSuchArchiverException e) {
114 unArchiver = archiverManager.getUnArchiver(file);
115 logger.debug("Found unArchiver: " + unArchiver.getClass().getName() + " by file extension: " + file);
116 }
117
118 if (encoding != null && unArchiver instanceof ZipUnArchiver) {
119 ((ZipUnArchiver) unArchiver).setEncoding(encoding);
120 logger.info("Unpacks '" + type + "' with encoding '" + encoding + "'.");
121 }
122
123 unArchiver.setIgnorePermissions(ignorePermissions);
124
125 unArchiver.setSourceFile(file);
126
127 unArchiver.setDestDirectory(location);
128
129 if ((excludes != null && !excludes.isEmpty()) || (includes != null && !includes.isEmpty())) {
130
131
132
133 IncludeExcludeFileSelector[] selectors =
134 new IncludeExcludeFileSelector[] {new IncludeExcludeFileSelector()};
135
136 if (excludes != null && !excludes.isEmpty()) {
137 selectors[0].setExcludes(excludes.split(","));
138 }
139
140 if (includes != null && !includes.isEmpty()) {
141 selectors[0].setIncludes(includes.split(","));
142 }
143
144 unArchiver.setFileSelectors(selectors);
145 }
146
147 unArchiver.setFileMappers(fileMappers);
148
149 unArchiver.extract();
150 } catch (NoSuchArchiverException e) {
151 throw new MojoExecutionException("Unknown archiver type", e);
152 } catch (ArchiverException e) {
153 throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location, e);
154 }
155 buildContext.refresh(location);
156 }
157
158 private void logUnpack(Log logger, File file, File location, String includes, String excludes) {
159 if (logger.isInfoEnabled()) {
160 return;
161 }
162
163 StringBuilder msg = new StringBuilder();
164 msg.append("Unpacking ");
165 msg.append(file);
166 msg.append(" to ");
167 msg.append(location);
168
169 if (includes != null && excludes != null) {
170 msg.append(" with includes \"");
171 msg.append(includes);
172 msg.append("\" and excludes \"");
173 msg.append(excludes);
174 msg.append("\"");
175 } else if (includes != null) {
176 msg.append(" with includes \"");
177 msg.append(includes);
178 msg.append("\"");
179 } else if (excludes != null) {
180 msg.append(" with excludes \"");
181 msg.append(excludes);
182 msg.append("\"");
183 }
184
185 logger.info(msg.toString());
186 }
187 }