com.zusmart.maven.plugin.RepackageMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zusmart-maven-plugin Show documentation
Show all versions of zusmart-maven-plugin Show documentation
项目打包maven插件,使用此插件可直接打包成zip文件,包含引用及启动脚本,可直接用户生产
The newest version!
package com.zusmart.maven.plugin;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil;
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class RepackageMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${project.build.directory}", required = true)
private File outputDirectory;
@Parameter(defaultValue = "${project.build.finalName}", readonly = true)
private String finalName;
@Parameter
private List requiresUnpack;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if ("pom".equals(this.project.getPackaging())) {
this.getLog().debug("repackage goal could not be applied to pom project.");
return;
}
try {
this.repackage();
} catch (IOException e) {
this.getLog().error(e.getMessage(), e);
}
}
private void repackage() throws MojoExecutionException, IOException {
File targetFile = this.getTargetFile();
File targetZipFile = new File(targetFile.getParentFile(), this.finalName + ".zip");
if (targetZipFile.exists()) {
targetZipFile.delete();
}
targetZipFile.createNewFile();
ZipOutputStream ziozos = new ZipOutputStream(new FileOutputStream(targetZipFile));
this.writeClasspath(targetFile, ziozos);
this.writeLibraries(targetFile, ziozos);
this.writeResources(targetFile, ziozos);
this.writeScripts(targetFile, ziozos);
ziozos.closeEntry();
ziozos.close();
}
private void writeScripts(File targetFile, ZipOutputStream ziozos) throws IOException {
this.writeWindowsScripts(targetFile, ziozos);
this.writeLinuxScripts(targetFile, ziozos);
}
private void writeWindowsScripts(File targetFile, ZipOutputStream ziozos) throws IOException {
ziozos.putNextEntry(new ZipEntry(this.finalName + "/bin/server.cmd"));
ziozos.write(IOUtil.toByteArray(this.getClass().getResourceAsStream("/server.cmd")));
}
private void writeLinuxScripts(File targetFile, ZipOutputStream ziozos) throws IOException {
ziozos.putNextEntry(new ZipEntry(this.finalName + "/bin/server.sh"));
ziozos.write(IOUtil.toByteArray(this.getClass().getResourceAsStream("/server.sh")));
}
private void writeClasspath(File targetFile, ZipOutputStream ziozos) throws IOException {
List resources = this.project.getResources();
if (null == resources || resources.isEmpty()) {
return;
}
this.appendClasses(this.finalName + "/classes", this.outputDirectory.getPath() + "/classes", ziozos);
}
private void appendClasses(String parentPath, String path, ZipOutputStream ziozos) throws IOException {
File parent = new File(path);
if (!parent.exists()) {
return;
}
File[] files = parent.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory() || pathname.getName().endsWith(".class");
}
});
if (null == files || files.length == 0) {
return;
}
for (File file : files) {
if (file.isDirectory()) {
this.appendClasses(parentPath + "/" + file.getName(), file.getPath(), ziozos);
} else if (file.getName().endsWith(".class")) {
ziozos.putNextEntry(new ZipEntry(parentPath + "/" + file.getName()));
ziozos.write(IOUtil.toByteArray(new FileInputStream(file)));
}
}
}
private void writeLibraries(File targetFile, ZipOutputStream ziozos) throws FileNotFoundException, IOException {
Set artifacts = this.project.getArtifacts();
if (null != artifacts && artifacts.size() > 0) {
for (Artifact artifact : artifacts) {
File artifactFile = artifact.getFile();
ziozos.putNextEntry(new ZipEntry(this.finalName + "/lib/" + artifactFile.getName()));
ziozos.write(IOUtil.toByteArray(new FileInputStream(artifactFile)));
}
}
}
private void writeResources(File targetFile, ZipOutputStream ziozos) throws IOException {
List resources = this.project.getResources();
if (null != resources && resources.size() > 0) {
for (Resource resource : resources) {
this.appendResource(this.finalName + "/conf", resource.getDirectory(), ziozos);
}
}
}
private void appendResource(String parentPath, String path, ZipOutputStream ziozos) throws IOException {
File parent = new File(path);
if (!parent.exists()) {
return;
}
File[] files = parent.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory() || !pathname.getName().endsWith(".class");
}
});
if (null == files || files.length == 0) {
return;
}
for (File file : files) {
if (file.isDirectory()) {
this.appendResource(parentPath + "/" + file.getName(), file.getPath(), ziozos);
} else {
ziozos.putNextEntry(new ZipEntry(parentPath + "/" + file.getName()));
ziozos.write(IOUtil.toByteArray(new FileInputStream(file)));
}
}
}
private File getTargetFile() {
if (!this.outputDirectory.exists()) {
this.outputDirectory.mkdirs();
}
return new File(this.outputDirectory, this.finalName + "." + this.project.getArtifact().getArtifactHandler().getExtension());
}
}