Skip to content

Commit

Permalink
Remove remaining unpacker code
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Jan 18, 2018
1 parent 95f6b9f commit 107c386
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 180 deletions.
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,11 @@
</dependency>

<!-- plexus -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.24</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>file-management</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,58 +36,41 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.StringUtils;

/**
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
* @version $Id$
*/
public abstract class AbstractDependencyMojo
extends AbstractMojo {
/**
* To look up Archiver/UnArchiver implementations
*/
@Component
private ArchiverManager archiverManager;

/**
* <p>
* will use the jvm chmod, this is available for user and all level group level will be ignored
* </p>
* <b>since 2.6 is on by default</b>
*
*
* @since 2.5.1
*/
@Parameter(property = "dependency.useJvmChmod", defaultValue = "true")
private boolean useJvmChmod = true;

/**
* ignore to set file permissions when unpacking a dependency
*
*
* @since 2.7
*/
@Parameter(property = "dependency.ignorePermissions", defaultValue = "false")
Expand Down Expand Up @@ -160,13 +143,6 @@ public final void execute()
protected abstract void doExecute()
throws MojoExecutionException, MojoFailureException;

/**
* @return Returns the archiverManager.
*/
public ArchiverManager getArchiverManager() {
return this.archiverManager;
}

/**
* Does the actual link of the file and logging.
*
Expand Down Expand Up @@ -201,119 +177,12 @@ protected void linkFile(File artifact, File destFile)
}
}

protected void unpack(Artifact artifact, File location, String encoding)
throws MojoExecutionException {
unpack(artifact, location, null, null, encoding);
}

/**
* Unpacks the archive file.
*
* @param artifact File to be unpacked.
* @param location Location where to put the unpacked files.
* @param includes Comma separated list of file patterns to include i.e. <code>**&#47;.xml,
* **&#47;*.properties</code>
* @param excludes Comma separated list of file patterns to exclude i.e. <code>**&#47;*.xml,
* **&#47;*.properties</code>
* @param encoding Encoding of artifact. Set {@code null} for default encoding.
* @throws MojoExecutionException In case of errors.
*/
protected void unpack(Artifact artifact, File location, String includes, String excludes, String encoding)
throws MojoExecutionException {
unpack(artifact, artifact.getType(), location, includes, excludes, encoding);
}

protected void unpack(Artifact artifact, String type, File location, String includes, String excludes,
String encoding)
throws MojoExecutionException {
File file = artifact.getFile();
try {
logUnpack(file, location, includes, excludes);

location.mkdirs();
if (!location.exists()) {
throw new MojoExecutionException("Location to write unpacked files to could not be created: "
+ location);
}

if (file.isDirectory()) {
// usual case is a future jar packaging, but there are special cases: classifier and other packaging
// other packaging
throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, "
+ "unpack should be executed after packaging: see MDEP-98.");
}

UnArchiver unArchiver;

try {
unArchiver = archiverManager.getUnArchiver(type);
getLog().debug("Found unArchiver by type: " + unArchiver);
} catch (NoSuchArchiverException e) {
unArchiver = archiverManager.getUnArchiver(file);
getLog().debug("Found unArchiver by extension: " + unArchiver);
}

if (encoding != null && unArchiver instanceof ZipUnArchiver) {
((ZipUnArchiver) unArchiver).setEncoding(encoding);
getLog().info("Unpacks '" + type + "' with encoding '" + encoding + "'.");
}

unArchiver.setUseJvmChmod(useJvmChmod);

unArchiver.setIgnorePermissions(ignorePermissions);

unArchiver.setSourceFile(file);

unArchiver.setDestDirectory(location);

if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
// Create the selectors that will filter
// based on include/exclude parameters
// MDEP-47
IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };

if (StringUtils.isNotEmpty(excludes)) {
selectors[0].setExcludes(excludes.split(","));
}

if (StringUtils.isNotEmpty(includes)) {
selectors[0].setIncludes(includes.split(","));
}

unArchiver.setFileSelectors(selectors);
}
if (this.silent) {
silenceUnarchiver(unArchiver);
}

unArchiver.extract();
} catch (NoSuchArchiverException e) {
throw new MojoExecutionException("Unknown archiver type", e);
} catch (ArchiverException e) {
throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location + "\r\n"
+ e.toString(), e);
}
}

private void silenceUnarchiver(UnArchiver unArchiver) {
// dangerous but handle any errors. It's the only way to silence the unArchiver.
try {
Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses("logger", unArchiver.getClass());

field.setAccessible(true);

field.set(unArchiver, this.getLog());
} catch (Exception e) {
// was a nice try. Don't bother logging because the log is silent.
}
}

/**
* @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
* repositories, used to resolve artifacts.
*/
public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest() {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
final ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());

buildingRequest.setRemoteRepositories(remoteRepositories);

Expand All @@ -327,13 +196,6 @@ public MavenProject getProject() {
return this.project;
}

/**
* @param archiverManager The archiverManager to set.
*/
public void setArchiverManager(ArchiverManager archiverManager) {
this.archiverManager = archiverManager;
}

public boolean isUseJvmChmod() {
return useJvmChmod;
}
Expand Down Expand Up @@ -361,33 +223,4 @@ public void setSilent(boolean silent) {
}
}

private void logUnpack(File file, File location, String includes, String excludes) {
if (!getLog().isInfoEnabled()) {
return;
}

StringBuilder msg = new StringBuilder();
msg.append("Unpacking ");
msg.append(file);
msg.append(" to ");
msg.append(location);

if (includes != null && excludes != null) {
msg.append(" with includes \"");
msg.append(includes);
msg.append("\" and excludes \"");
msg.append(excludes);
msg.append("\"");
} else if (includes != null) {
msg.append(" with includes \"");
msg.append(includes);
msg.append("\"");
} else if (excludes != null) {
msg.append(" with excludes \"");
msg.append(excludes);
msg.append("\"");
}

getLog().info(msg.toString());
}
}

0 comments on commit 107c386

Please sign in to comment.