-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MDEP-859] Code refactor - UnpackUtil
Methods for unpack artifacts are in AbstractDependencyMojo but are only used by unpack and unpack-dependencies mojos. We can create separate component and use only where is needed.
- Loading branch information
1 parent
7c75bc9
commit 3fb3669
Showing
10 changed files
with
262 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
def buildLog = new File( basedir, 'build.log' ) | ||
assert buildLog.exists() | ||
assert buildLog.length() != 0 | ||
assert buildLog.text.contains( "[DEBUG] Found unArchiver by type: " ) | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
def buildLog = new File( basedir, 'build.log' ) | ||
assert buildLog.exists() | ||
assert buildLog.length() != 0 | ||
assert buildLog.text.contains( "[DEBUG] Found unArchiver: org.apache.maven.archiver.LogUnArchiver by type: custom-ear" ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,8 @@ | |
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
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; | ||
|
@@ -36,26 +33,13 @@ | |
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.filemappers.FileMapper; | ||
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector; | ||
import org.codehaus.plexus.util.FileUtils; | ||
import org.codehaus.plexus.util.ReflectionUtils; | ||
import org.sonatype.plexus.build.incremental.BuildContext; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Brian Fox</a> | ||
*/ | ||
public abstract class AbstractDependencyMojo extends AbstractMojo { | ||
/** | ||
* To look up Archiver/UnArchiver implementations | ||
*/ | ||
@Component | ||
private ArchiverManager archiverManager; | ||
|
||
/** | ||
* For IDE build support | ||
|
@@ -72,14 +56,6 @@ public abstract class AbstractDependencyMojo extends AbstractMojo { | |
@Parameter(defaultValue = "false") | ||
private boolean skipDuringIncrementalBuild; | ||
|
||
/** | ||
* ignore to set file permissions when unpacking a dependency | ||
* | ||
* @since 2.7 | ||
*/ | ||
@Parameter(property = "dependency.ignorePermissions", defaultValue = "false") | ||
private boolean ignorePermissions; | ||
|
||
/** | ||
* POM | ||
*/ | ||
|
@@ -155,13 +131,6 @@ public final void execute() throws MojoExecutionException, MojoFailureException | |
*/ | ||
protected abstract void doExecute() throws MojoExecutionException, MojoFailureException; | ||
|
||
/** | ||
* @return Returns the archiverManager. | ||
*/ | ||
public ArchiverManager getArchiverManager() { | ||
return this.archiverManager; | ||
} | ||
|
||
/** | ||
* Does the actual copy of the file and logging. | ||
* | ||
|
@@ -188,146 +157,6 @@ protected void copyFile(File artifact, File destFile) throws MojoExecutionExcept | |
} | ||
} | ||
|
||
/** | ||
* @param artifact {@link Artifact} | ||
* @param location The location. | ||
* @param encoding The encoding. | ||
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting | ||
* shall happen. | ||
* @throws MojoExecutionException in case of an error. | ||
*/ | ||
protected void unpack(Artifact artifact, File location, String encoding, FileMapper[] fileMappers) | ||
throws MojoExecutionException { | ||
unpack(artifact, location, null, null, encoding, fileMappers); | ||
} | ||
|
||
/** | ||
* 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>**/.xml, | ||
* **/*.properties</code> | ||
* @param excludes Comma separated list of file patterns to exclude i.e. <code>**/*.xml, | ||
* **/*.properties</code> | ||
* @param encoding Encoding of artifact. Set {@code null} for default encoding. | ||
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting | ||
* shall happen. | ||
* @throws MojoExecutionException In case of errors. | ||
*/ | ||
protected void unpack( | ||
Artifact artifact, | ||
File location, | ||
String includes, | ||
String excludes, | ||
String encoding, | ||
FileMapper[] fileMappers) | ||
throws MojoExecutionException { | ||
unpack(artifact, artifact.getType(), location, includes, excludes, encoding, fileMappers); | ||
} | ||
|
||
/** | ||
* @param artifact {@link Artifact} | ||
* @param type The type. | ||
* @param location The location. | ||
* @param includes includes list. | ||
* @param excludes excludes list. | ||
* @param encoding the encoding. | ||
* @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting | ||
* shall happen. | ||
* @throws MojoExecutionException in case of an error. | ||
*/ | ||
protected void unpack( | ||
Artifact artifact, | ||
String type, | ||
File location, | ||
String includes, | ||
String excludes, | ||
String encoding, | ||
FileMapper[] fileMappers) | ||
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 | ||
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.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.setFileMappers(fileMappers); | ||
|
||
unArchiver.extract(); | ||
} catch (NoSuchArchiverException e) { | ||
throw new MojoExecutionException("Unknown archiver type", e); | ||
} catch (ArchiverException e) { | ||
throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location, e); | ||
} | ||
buildContext.refresh(location); | ||
} | ||
|
||
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. | ||
|
@@ -359,13 +188,6 @@ public MavenProject getProject() { | |
return this.project; | ||
} | ||
|
||
/** | ||
* @param archiverManager The archiverManager to set. | ||
*/ | ||
public void setArchiverManager(ArchiverManager archiverManager) { | ||
this.archiverManager = archiverManager; | ||
} | ||
|
||
/** | ||
* @return {@link #skip} | ||
*/ | ||
|
@@ -399,34 +221,4 @@ public void setSilent(boolean silent) { | |
setLog(new DependencySilentLog()); | ||
} | ||
} | ||
|
||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.