加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在pom.xml中设置classpath?

发布时间:2020-12-14 19:24:07 所属栏目:Java 来源:网络整理
导读:使用maven运行测试时,我想在屏幕上看到输出. 将log4j.xml放入我的项目的目录(src / test / resources / cfg /)之后,我更新了我的pom以包含 235 plugin236 groupIdorg.apache.maven.plugins/groupId237 artifactIdmaven-surefire-plugin/artifactId238 config
使用maven运行测试时,我想在屏幕上看到输出.

将log4j.xml放入我的项目的目录(src / test / resources / cfg /)之后,我更新了我的pom以包含

235             <plugin>
236                 <groupId>org.apache.maven.plugins</groupId>
237                 <artifactId>maven-surefire-plugin</artifactId>
238                 <configuration>
239                     <skipTests>false</skipTests>
240                     <excludes>
241                         <exclude>**/*$*.java</exclude>
242                     </excludes>
243                         <systemProperties>
244                            <property>
245                               <name>-Dlog4j.configuration</name>
246                               <value>file:src/test/resources/cfg/log4j.xml</value>
247                            </property>
248                         </systemProperties>
249                     <additionalClasspathElements>
250                         <additionalClasspathElement>src/test/resources/cfg/</additionalClasspathElement>
251                     </additionalClasspathElements>
252                 </configuration>
253             </plugin>

以上不起作用.测试运行时,我仍然看到以下消息

log4j:WARN Please initialize the log4j system properly.

对于记录,以下是log4j

1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3 
  4 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  5   <appender name="console" class="org.apache.log4j.ConsoleAppender">
  6     <param name="Target" value="System.out"/>
  7     <layout class="org.apache.log4j.PatternLayout">
  8       <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
  9     </layout>
 10   </appender>
 11 
 12   <root>
 13     <priority value ="debug" />
 14     <appender-ref ref="console" />
 15   </root>
 16 
 17 </log4j:configuration>

我错过了什么?

解决方法

我有几点意见.首先,我建议您将任何与测试相关的log4j,logback配置文件直接放在src / test / resources中.这样它们总是被添加到类路径中,它只是起作用.

但是,如果您需要将文件放在src / test / resources / cfg中并想使用classpatlelements,那么请注意documentation的建议

But,if you must,you can use the additionalClasspathElements element to add custom resources/jars to your classpath. This will be treated as an absolute file system path,so you may want use ${basedir} or another property combined with a relative path.

所以尝试:

<additionalClasspathElement>${basedir}/src/test/resources/cfg/</additionalClasspathElement>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读