maven插件的使用

时间:2023-03-09 02:56:52
maven插件的使用

maven插件官网:

https://maven.apache.org/plugins/index.html

1.JDK插件的使用

     <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

注意:修改JDK版本后,项目可能会报错,此时需要更新maven项目。

2.配置打包项目源码包的插件

 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<!-- 完成绑定.执行完打包后执行 -->
<executions>
<execution>
<goals>
<goal>jar-no-fork</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>

注意:需要更新项目。执行maven install,在仓库中,即可查看到该项目的源码。如图:

maven插件的使用

3.tomcat7插件

 <!-- 3.tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<server>tomcat7</server>
</configuration>
</plugin>

说明:

port:服务器端口号

server:服务器名称

运行示例:

在webapp下创建一个jsp,jsp头部报错,需要在pom.xml中引入servert的依赖,如:

 <!-- 依赖servlet的api -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<!-- privided依赖范围,编译时有效,但是运行发布的时候是无效的。 -->
<scope>provided</scope>
</dependency>

运行:右键项目→Run As→Maven build...→输入tomcat7:run

maven插件的使用

服务器启动:

maven插件的使用

在浏览器地址栏中输入:http://localhost:8080/sm1234-web/hello.jsp

若不加入<scope>provided</scope>,会发现如下错误:

maven插件的使用

错误:

java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest"

原因分析:当项目加载servlet-api时,整个环境中还存在另外一个servlet-api。该问题在实际开发中也会经常遇到。

结论:servlet-api只需要在编译时使用,而运行时无需使用。修改后,查看运行结果:

maven插件的使用