Maven 插件
本文最后更新于:2024年9月8日 晚上
Maven 插件
Spring Boot
spring-boot-maven-plugin
插件用来打包,我们只需要在pom.xml
中加入以下配置。
1 2 3 4 5 6 7 8
| <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
- 如果不喜欢默认的项目名+版本号作为文件名,可以加一个配置指定文件名:
1 2 3 4 5 6 7
| <project ...> ... <build> <finalName>awesome-app</finalName> ... </build> </project>
|
Jetty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.39.v20210325</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> <webAppSourceDirectory>web/</webAppSourceDirectory> <webApp> <contextPath>/</contextPath> </webApp> </configuration> </plugin>
|
Tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <plugin> <groupId>org.opoo.maven</groupId> <artifactId>tomcat9-maven-plugin</artifactId> <version>3.0.0</version> <configuration> <port>8080</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat9</server> </configuration> </plugin>
|
1 2 3 4 5 6
| tomcat:deploy 部署一个web war包。 tomcat:reload 重新加载web war包。 tomcat:start 启动。 tomcat:stop 停止。 tomcat:undeploy 停止一个war包。 tomcat:run 启动嵌入式tomcat ,并运行当前项目。
|
远程部署
1 2 3 4 5
| <server> <id>tomcat7</id> <username>tomcat</username> <password>tomcat</password> </server>
|
引入tomcat7插件
- maven中关于tomcat的插件有tomcat6插件和tomcat7插件,普遍使用的是tomcat7插件,在
pom.xml
中添加以下片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <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>tomcat7</server> <path>/</path> <update>true</update> <username>tomcat</username> <password>tomcat</password> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins>
|
- 配置完成,接下来运行
run as maven build
,然后在Goals中填上tomcat7:deploy
,运行项目,实现maven热部署了,看到以下信息提示,就表示执行成功了。
1 2 3 4 5 6 7 8 9
| Copy[INFO] tomcatManager status code:200, ReasonPhrase:OK [INFO] OK - Deployed application at context path / [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.560 s [INFO] Finished at: 2019-04-18T20:37:57+08:00 [INFO] Final Memory: 21M/222M [INFO] ------------------------------------------------------------------------
|
注意
- 部署项目前要先启动tomcat服务器。
- 在执行tomcat7:deploy命令时注意jre版本的配置,JDK版本选择1.7
- 首次执行选择第一个maven build,非首次执行选择第二个maven build 命令执行tomcat7:redeploy
screw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <plugin> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-maven-plugin</artifactId> <version>1.0.5</version> <dependencies> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies> <configuration> <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName> <jdbcUrl>jdbc:mysql://localhost:3306/test</jdbcUrl> <username>root</username> <password>123456</password> <fileType>WORD</fileType> <title>数据库文档</title> <fileName>测试文档名称</fileName> <description>数据库文档生成</description> <version>${project.version}</version> <openOutputDir>false</openOutputDir> <produceType>freemarker</produceType> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
|