maven插件

tomcat7-maven-plugin插件

1. 不通过外部的tomcat 直接将tomcat核心内嵌在项目中,tomcat7:run 命令就可以启动项目,可以直接访问 localhost:8080/Demo

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>7.0.47</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

2. 热部署(tomcat是活的) tomcat7:deploy 命令就可以将项目发布到活着的tomcat下 再次发布使用命令 tomcat7:redeploy

tomcat-user.xml

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="root" password="root" roles="manager-gui,manager-script"/>

pom.xml

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <username>root</username>
                    <password>root</password>
                </configuration>
            </plugin>

cargo-maven-plugin插件

1.部署到本地Web容器

1.1 standalone模式

 

 在standalone模式,Cargo会从Web容器的安装目录复制一份配置到用户指定的目录,然后在此基础上部署应用,每次重新构建的时候,这个目录都会被清空,所有配置被重新生成

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.4.9</version>
    <configuration>
        <container>
            <containerId>tomcat7x</containerId>
            <home>/usr/local/devtools/apache-tomcat-7.0.55</home>
        </container>
        <configuration>
            <type>standalone</type>
            <home>${project.build.directory}/tomcat7x</home>
            <properties>
                <!-- 更改监听端口 -->
                <cargo.servlet.port>8088</cargo.servlet.port>
            </properties>
        </configuration>
    </configuration>
</plugin>

然后用mvn cargo:run启动,关于cargo:run于cargo:start有什么区别,后续会讲到。

1.2 existing模式

 

在existing模式下,用户需要指定现有的web容器配置目录,然后Cargo会直接使用这些配置并将应用部署到其对应的位置

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.4.9</version>
    <configuration>
        <container>
            <containerId>tomcat7x</containerId>
            <home>/usr/local/devtools/apache-tomcat-7.0.55</home>
        </container>
        <configuration>
            <type>existing</type>
            <home>/usr/local/devtools/apache-tomcat-7.0.55</home>
        </configuration>
    </configuration>
</plugin>

然后运行cargo:run之后在对应的tomcat的webapps目录下能够看到被部署的应用

2.部署到远程Web容器

 

这里注意在远程部署模式下,Container元素的type子元素的值必须为remote,如果不指定,Cargo会默认使用installed,并寻找对应的容器安装目录或者安装包,一般我们远程部署的服务器上都有设定好的web容器了,并不需要再区安装。

pom.xml

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.4.9</version>
    <configuration>
        <container>
            <containerId>tomcat7x</containerId>
            <type>remote</type>
        </container>
        <configuration>
            <type>runtime</type>
            <properties>
                <cargo.tomcat.manager.url>http://localhost:8080/manager/text</cargo.tomcat.manager.url>
                <cargo.remote.username>admin</cargo.remote.username>
                <cargo.remote.password>password</cargo.remote.password>
            </properties>
        </configuration>
        <deployables>
            <deployable>
                <groupId>io.steveguoshao</groupId>
                <artifactId>webapp</artifactId>
                <type>war</type>
                <properties>
                    <context>/${project.artifactId}</context>
                </properties>
                <!-- 可选:验证是否部署成功 -->
                <pingURL>http://localhost:8080/webapp</pingURL>
                <!-- 可选:验证超时时间,默认是120000 毫秒 -->
                <pingTimeout>60000</pingTimeout>
            </deployable>
        </deployables>
    </configuration>
    <executions>
        <execution>
            <id>verify-deployer</id>
            <phase>install</phase>
            <goals>
                <goal>deployer-redeploy</goal>
            </goals>
        </execution>
        <execution>
            <id>clean-deployer</id>
            <phase>clean</phase>
            <goals>
                <goal>deployer-undeploy</goal>
            </goals>
        </execution>
    </executions>
</plugin>

在tomcat7的conf/tomcat-users.xml中增加角色和用户

 <role rolename="manager-gui"/>  
 <role rolename="manager-script"/>  
 <role rolename="manager-jmx"/>    
 <role rolename="manager-status"/>  
 <role rolename="admin-gui"/>  
 <user username="admin" password="password" roles="admin-gui,manager-gui,manager-script,manager-status"/> 

另外还有一点要注意的是url,tomcat7是

http://localhost:8080/manager/text

而tomcat6是

http://localhost:8080/manager/html

配置好之后就可以运行mvn cargo:redeploy 来部署应用了(必须保证tomcat是running状态,否则没法部署),如果容器中已经部署的当前应用,Cargo会先卸载掉原来的应用,然后再重新部署。

mvn cargo:start命令完成WAR包部署后,启动服务器,然后会将服务器立即关掉;

mvn cargo:run命令完成WAR包部署后,启动服务器,直到你Ctrl+C将服务器关掉

mvn cargo:stop命令关闭服务器

maven help插件

 Maven Help 插件有四个目标。前三个目标是—— active-profiles , effective-pom 和effective-settings —— 描述一个特定的项目,它们必须在项目的目录下运行。 最后一个目标—— describe ——相对比较复杂,展示某个插件或者插件目标的相关信息。

help:active-profiles
列出当前构建中活动的Profile(项目的,用户的,全局的)
help:effective-pom
显示当前构建的实际POM,包含活动的Profile
help:effective-settings
打印出项目的实际settings, 包括从全局的settings和用户级别settings继承的配置
help:describe -Dplugin=groupId:artifact[:version]
-Dfull
描述插件的属性。它不需要在项目目录下运行。但是你必须提供你想要描述插件的 groupId 和 artifactId
你可以传入插件的前缀(如-Dplugin=help 插件就是 maven-help-plugin )
有时候这些信息显得太多了。这时候你可以获取单个目标的信息,设置 mojo 参数和 plugin 参数。下面的命令列出了Compiler 插件的 compile 目标的所有信息
$ mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull
注意! 什么? Mojo ?在Maven里面, 一个插件目标也被认为是一个 “Mojo”mojo是goal的具体实现;mojo才是做具体事情的,可以简单理解mojo为goal的实现类,它继承于AbstractMojo,有一 个execute方法,goal等的定义都是通过在mojo里定义一些注释的anotation来实现的,
 "-D<name>=<value>"这种格式不是Maven定义的,它其实是Java用来设置系统属性的方式,可以通过“java -help”查看Java的解释。Maven的bin目录下的脚本文件仅仅是把属性传入Java而已
原文地址:https://www.cnblogs.com/woms/p/5767901.html