【问题标题】:Maven project is getting compiled by Eclipse Update Project but not by pom.xml?Maven 项目是由 Eclipse 更新项目编译的,而不是由 pom.xml 编译的?
【发布时间】:2018-07-07 03:07:59
【问题描述】:

我是 Maven 新手,每次通过 pom.xml 或 testng.xml 运行之前都必须更新我的 Maven 项目

每当我在 Maven clean、Update Project 和 Maven Test 之后运行我的项目时,它都能正常工作。但是,如果我尝试直接使用 clean 运行并进行测试,它会给我类路径错误。 清洁目标是清除目标,然后 maven 无法编译,但是,如果我通过 Eclipse 更新项目,则不会出现问题。 尝试更新 testng 和 maven surefire 插件。下面是我的pom

<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>Complete_POM</groupId>
    <artifactId>Complete_POM</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
<suiteXmlFile>testng.xml</suiteXmlFile>
    </properties>
    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.13.1</version>
    <scope>test</scope>
</dependency>           <!https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
        <dependency>
            <groupId>org.uncommons</groupId>
            <artifactId>reportng</artifactId>
            <version>1.1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency>
        <!-- Dependency for POI API -->
        <!-- http://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.6</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.6</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.1</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.3.0</version>
        </dependency>

        <!-- http://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.6</version>
        </dependency>
    </dependencies>

<build>
<plugins>
 <plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>

<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>

</configuration>
</plugin>

</plugins>

        <testResources>
            <testResource>
                <directory>src/test/resources/Properties</directory>
                <includes>
                    <include>log4j.properties</include>
                </includes>
            </testResource>
        </testResources>
    </build>
</project>

和TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="Listeners.CustomListeners"></listener>
</listeners>
  <test name="LoginTest">
    <classes>
      <class name="TestCases.LoginTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

非常感谢任何帮助

【问题讨论】:

  • 这是编译错误,您需要通过添加所需的 jar 并更正代码来解决此问题。
  • @Ravi 我已经添加了罐子,问题是如果我通过右键单击选项更新我的 maven 项目,它可以完美运行,但我必须每次都这样做。
  • 小更新 - 每当我清理项目时,我都会在连续运行中遇到这个问题。
  • 很明显,你得到了编译错误。你到底是如何在编译错误的情况下运行你的程序的?
  • @Ravi 我同意我收到编译错误,但这仅在我使用 maven clean 然后测试时发生。如果我更新项目然后 maven 测试,一切正常,没有任何编译错误。就像 maven clean 正在撤消通过更新项目过程所做的更改:(。希望我现在能够清除问题。

标签: java eclipse maven testing pom.xml


【解决方案1】:

在你的pom.xml 你有这个部分:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.13.1</version>
    <scope>test</scope>
</dependency> 

你必须更改scope才能编译,所以它会

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.13.1</version>
    <scope>compile</scope>
</dependency> 

【讨论】:

  • 试过了,还是一样的问题
猜你喜欢
  • 2011-04-18
  • 2012-08-06
  • 2021-06-25
  • 2016-10-27
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 2018-03-20
  • 2013-11-29
相关资源
最近更新 更多