当一堆Maven项目互相依赖须要联调时,每每会被如下问题所困扰:java
一、被依赖的项目修改,咋debug时没效果?web
二、被依赖项目,哪怕直接依赖在工程中,有时候代码也提示找不到源码。apache
因此我采起了嵌入Jetty容器+Maven命令配合的方式执行。api
解决方案:bash
一、创建声明Modules的pom.xmlapp
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx</groupId> <artifactId>xxx</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>xxx</name> <modules> <module>../x1</module> <module>../x2</module> </modules> </project>
并在此执行一次eclipse
mvn eclipse:eclipse
二、项目pom.xml中加入jetty和servlet相关依赖webapp
<!-- jetty相关 --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.2.19.v20160908</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>9.2.19.v20160908</version> </dependency> <!-- end -->
三、加入Jetty容器启动main方法jsp
package com.tuling.api; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class WebServer { public static void main(String[] args) throws Exception { String projectPath = System.getProperty("user.dir"); String[] fileName = projectPath.split("\\\\"); // 工程名称 String projectName = fileName[fileName.length - 1]; // 如项目在trunk下,能够重写项目名称,不然会识别为trunk projectName = "api"; // web资源路径 String WebRoot = "src/main/webapp"; // 端口号 int port = 8080; Server server = new Server(port); WebAppContext webapp = new WebAppContext(); webapp.setDefaultsDescriptor("src/main/resources/webdefault.xml"); webapp.setDescriptor("src/main/webapp/WEB-INF/web.xml"); webapp.setContextPath("/" + projectName); webapp.setWar(WebRoot); server.setHandler(webapp); server.start(); server.join(); } }
四、Eclipse中的设置maven
a)把项目引入(废话……)
b)建立编译启动项
compile test-compile
这一步主要目的是导入profile中的变量。
c)启动刚才编写的main方法