改了一下nutzboot-maven-plugin的WarMojo支持了这种方式,贴出来给给需要的人
package org.nutz.boot.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.AttachedArtifact;
import org.nutz.lang.Encoding;
import org.nutz.lang.Files;
import org.nutz.lang.Streams;
import org.nutz.lang.Strings;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Mojo(name = "war")
public class WarMojo extends AbstractNbMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(property = "nutzboot.mainPackage", defaultValue = "")
protected String mainPackage;
private String webXml;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (Strings.isBlank(mainClass)) {
mainClass = AbstractNbMojo.searchMainClass(target, getLog());
}
File jarFile = project.getArtifact().getFile();
org.apache.maven.plugin.logging.Log log = getLog();
log.info("Convert " + jarFile.getName());
webXml = "";;
File dstFile = new File(jarFile.getParentFile(), project.getArtifactId() + ".war");
try (ZipOutputStream dstZip = new ZipOutputStream(new FileOutputStream(dstFile), Encoding.CHARSET_UTF8)) {
File libDir = new File(jarFile.getParentFile() + "/lib");
File classesDir = new File(jarFile.getParentFile() + "/classes");
//写入lib
zip(dstZip, libDir, "WEB-INF/lib");
//写入classes
zip(dstZip, classesDir, "WEB-INF/classes");
// 最后,写入web.xml
webXml = createOrRewriteWebXml(webXml, mainClass);
dstZip.putNextEntry(new ZipEntry("WEB-INF/web.xml"));
dstZip.write(webXml.getBytes(Encoding.CHARSET_UTF8));
dstZip.closeEntry();
dstZip.finish();
dstZip.flush();
AttachedArtifact artifact = new AttachedArtifact(project.getArtifact(), "", "war", null);
artifact.setFile(dstFile);
project.addAttachedArtifact(artifact);
} catch (Exception e) {
log.error("convert fail", e);
}
}
protected String createOrRewriteWebXml(String sourceWebXml, String mainClass) {
StringBuilder sb = new StringBuilder();
sb.append("<listener><listener-class>org.nutz.boot.starter.servlet3.NbServletContextListener</listener-class></listener>\r\n");
sb.append("<context-param><param-name>nutzboot.mainClass</param-name><param-value>").append(mainClass).append("</param-value></context-param>\r\n");
if (Strings.isNotBlank(mainPackage)) {
sb.append("<context-param><param-name>nutzboot.mainPackage</param-name><param-value>").append(mainPackage).append("</param-value></context-param>\r\b");
}
if (Strings.isBlank(sourceWebXml)) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<web-app>\r\n " + sb + "\r\n</web-app>";
} else {
int index = sourceWebXml.indexOf('>', sourceWebXml.indexOf("<web-app" + 8)) + 1;
return sourceWebXml.substring(0, index) + sb + sourceWebXml.substring(index);
}
}
public void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] files = f.listFiles();
base = (base.length() == 0 ? "" : base + "/");
for (int i = 0; i < files.length; i++) {
if ("static".equals(files[i].getName()) || "webapp".equals(files[i].getName())) {
zip(out, files[i], "");
} else
zip(out, files[i], base + files[i].getName());
}
} else {
if ("web.xml".equals(f.getName()))
webXml = Files.read(f);
else {
boolean flag = true;
for (String pkg : Arrays.asList("jetty-",
"undertow-",
"catalina.",
"javax.servlet",
"javax-websocket",
"websocket-",
"tomcat-",
"nutzboot-starter-jetty",
"nutzboot-starter-tomcat",
"nutzboot-starter-undertow")) {
if (f.getName().startsWith(pkg)) {
flag = false;
}
}
if(flag) {
out.putNextEntry(new ZipEntry(base));
Streams.write(out, new FileInputStream(f));
}
}
}
}
}
pom加一个插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>