故障背景
今天尝试使用maven Jetty插件启动一个war 项目,无论执行maven的任何命令都会提示这样的警告信息。
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.xingyun:hello-world-sample:war:1.0-SNAPSHOT
[WARNING] The expression ${build.finalName} is deprecated. Please use ${project.build.finalName} instead.
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
故障分析
根据提示来看,我们可以看出
The expression ${build.finalName} is deprecated. Please use ${project.build.finalName} instead.
也就是说,
${build.finalName}
表达式已经过时了,请使用${project.build.finalName}
代替。
但是到底应该改哪里呢???
我刚开始找了半天,都没找到到底应该修改哪里。
直到今天晚上,偶然发现了答案,原来在这个地方。。。
<contextPath>/${build.finalName}</contextPath>
之前错误pom.xml 如下所示:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--设置本项目的groupId artifactId 和版本信息-->
<groupId>com.xingyun</groupId>
<artifactId>hello-world-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- 设置项目所使用的编码为UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--设置项目所用JDK版本,也可以使用低版本,建议使用JDK1.8以上版本-->
<java.version>12</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<!--是否跳过测试-->
<skipTests>true</skipTests>
<!--设置项目使用第三方依赖包,建议以后都采用这种方式,统一在一个地方声明版本号,这样对项目可以统一管理,方便以后升级-->
<jetty.maven.plugin.version>9.4.7.v20170914</jetty.maven.plugin.version>
</properties>
<build>
<!-- 定义项目的上下文 -->
<finalName>hello-world-sample</finalName>
<!-- 定义插件 -->
<plugins>
<!--定义这个插件允许我们使用mvn jetty:run命令运行我们当前这个项目-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.maven.plugin.version}</version>
<configuration>
<webApp>
<contextPath>/${build.finalName}</contextPath>
</webApp>
<stopKey>CTRL+C</stopKey>
<stopPort>8999</stopPort>
<scanIntervalSeconds>10</scanIntervalSeconds>
<scanTargets>
<scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
</scanTargets>
</configuration>
</plugin>
<!--该插件限定Maven 打包时所使用的版本,避免出现版本不匹配问题-->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
修复解决方案
修复成功的pom.xml配置如下:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--设置本项目的groupId artifactId 和版本信息-->
<groupId>com.xingyun</groupId>
<artifactId>hello-world-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- 设置项目所使用的编码为UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--设置项目所用JDK版本,也可以使用低版本,建议使用JDK1.8以上版本-->
<java.version>12</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<!--是否跳过测试-->
<skipTests>true</skipTests>
<!--设置项目使用第三方依赖包,建议以后都采用这种方式,统一在一个地方声明版本号,这样对项目可以统一管理,方便以后升级-->
<jetty.maven.plugin.version>9.4.7.v20170914</jetty.maven.plugin.version>
</properties>
<build>
<!-- 定义项目的上下文 -->
<finalName>hello-world-sample</finalName>
<!-- 定义插件 -->
<plugins>
<!--定义这个插件允许我们使用mvn jetty:run命令运行我们当前这个项目-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.maven.plugin.version}</version>
<configuration>
<webApp>
<contextPath>/${project.build.finalName}</contextPath>
</webApp>
<stopKey>CTRL+C</stopKey>
<stopPort>8999</stopPort>
<scanIntervalSeconds>10</scanIntervalSeconds>
<scanTargets>
<scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
</scanTargets>
</configuration>
</plugin>
<!--该插件限定Maven 打包时所使用的版本,避免出现版本不匹配问题-->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Tips:
你有没有好奇过如果不设置<finalName>hello-world-sample</finalName>
会发生什么呢?
- 如果不设置这个属性的话,那么默认
<contextPath>/${project.build.finalName}</contextPath>
的值将会是artifactId+version拼接在一起的结果,即:
http://127.0.0.1:8080/hello-world-sample-1.0-SNAPSHOT- 如果按照上面设置后就会变成这样
http://127.0.0.1:8080/hello-world-sample