NutzCN Logo
问答 nutzboot项目打包
发布于 1780天前 作者 hujun82589167 1797 次浏览 复制 上一个帖子 下一个帖子
标签:

一个nb项目打包成war时如何将依赖包以jar形式放置WEB-INF/lib下面,以下pom的写法只会把依赖包的class都放到WEB-INF/classes

<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.3</version>
                <configuration>
                    <additionalparam>-Xdoclint:none</additionalparam>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.nutz.boot</groupId>
                <artifactId>nutzboot-maven-plugin</artifactId>
                <version>${nutzboot.version}</version>
            </plugin>
        </plugins>
    </build>
6 回复

欢迎给nutzboot-maven-plugin提交pr

改了一下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>

@hujun82589167 试一下 nutz.scans.paths 配置项?

@Wizzercn 官方文档没这个scans的使用介绍,不会用,求指导。

添加回复
请先登陆
回到顶部