一、maven打war包到指定目录下
初步解决方法:
maven中更改target目录可以用<build>子目录<directory>,但是<directory>只能是相对于当前项目的目录,
虽然也能将war包打到项目外的目录下面,但是项目下会出现一个奇怪的很深的目录,有点不合适所以使用下面的插件来实现。
使用maven-dependency-plugin插件将war包打到指定的路径下:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <!--指定web.xml的路径 --> <webXml>WebRoot\WEB-INF\web.xml</webXml> <!--指定jsp、js、css的路劲 --> <warSourceDirectory>WebRoot</warSourceDirectory> </configuration> </plugin> <!--打war包到指定的目录下 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-war</id> <phase>package</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <version>${project.version}</version> <type>${project.packaging}</type> </artifactItem> </artifactItems> <!-- <outputDirectory>${dist.console.war.dir}</outputDirectory> --> <!--指定war包保存地址--> <outputDirectory>D:\Javaee\mypackage</outputDirectory> <includes> <include>*.war</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build> 二、项目打包到tomcat下
环境:apache-maven-3.0.5、apache-tomcat-7.0.39
配置流程如下:
1、 apache-tomcat-7.0.39配置C:\Program Files\apache-tomcat-7.0.39\conf\tomcat-users.xml,因为tomcat7默认情况下没有配置manager访问权限,所以这里需要在tomcat-users.xml加入用户以及权限
<tomcat-users> <role rolename="admin-gui"/> <role rolename="admin-script"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/> </tomcat-users>2、 apache-maven-3.0.5配置C:\Program Files\apache-maven-3.0.5\conf\settings.xml,为了让maven可以访问tomcat的权限,所以需要把如上创建的用户添加到settings.xml中,如下
<servers> <!-- 配置tomcat-/manager/text 访问权限 --> <server> <id>tomcat</id> <username>admin</username> <password>admin</password> </server> </servers>3、 工程目录下的pom.xml文件,加入build,并配置tomcat7的maven插件,如下配置
<build> <finalName>myApp</finalName> <!-- directory缺省情况下指向target --> <!--<directory>${basedir}/target</directory>--> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <!-- server、username、password对应maven的setting下的配置 --> <server>tomcat</server> <username>admin</username> <password>admin</password> <path>/${project.build.finalName}</path> <!-- war文件路径缺省情况下指向target --> <!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile>--> </configuration> </plugin> </plugins> </build> ${project.build.finalName}这个是根据xml的路径来标记的 ok 到这里配置结束了,下面看这么打包到tomcat下:
命令部署:
1、在部署之前,必须先启动tomcat7服务,C:\Program Files\apache-tomcat-7.0.39\bin\startup.bat
找到要部署的工程文件根目录下,执行如下maven命令
> mvn clean:install //clean是清理输出文件,install编译打包,在每次打包之前必须执行clean,才能保证发布为最新文件
> mvn tomcat7:redeploy //第一次发布 tomcat7:deploy,再次发布 tomcat7:redeploy
2、错误问题
Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path=%2FmyApp&war=
如上问题,有如下两个原因:
A、由于maven没有权限访问http://localhost:8080/manager/text,所以需要在apache-tomcat下的tomcat-users.xml增加用户权限,并配置于maven的setting文件中
B、由于maven-tomcat插件问题,通过http://search.maven.org/搜索tomcat-maven-plugin,发现最新版本之后,最后执行> mvn tomcat:redeploy,一直都会显示上面这个报错,这里如果是tomcat7,建议直接通过http://search.maven.org/搜索tomcat7-maven-plugin插件,执行> mvn tomcat7:redeploy,这样就部署成功了;如果tomcat6就直接通过http://search.maven.org/搜索tomcat6-maven-plugin
所以这里需要注意tomcat7-maven-plugin插件的引入,正确引入将解决以上问题
<groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> > mvn tomcat7:redeploy 即可完成部署
maven打包部署到tomcat下文章来自:
http://www.cnblogs.com/candle806/p/3785708.html
http://www.yiibai.com/maven/deploy-maven-based-war-file-to-tomcat.html