【问题标题】:Maven searching for an old version of Javax.Mail dependency (1.4.3) though I have the latest version of it (1.6.1) and throwing ClasNotFoundExceptionMaven 搜索旧版本的 Javax.Mail 依赖项(1.4.3)虽然我有它的最新版本(1.6.1)并抛出 ClasNotFoundException
【发布时间】:2018-04-05 18:28:26
【问题描述】:

我正在尝试在 Cucumber testRunner 类中编写一段代码,以在执行后通过电子邮件发送测试结果报告,但是我遇到了这个我无法解决的问题。我添加了 Javax.Mail 库(版本 1.6.1)的依赖项,并在 testRunner 中编写了以下方法,

@AfterClass
public static void teardown() throws UnknownHostException, IOException {       
    HTML_TestExecutionReportParser testExecutionStatusParser = new HTML_TestExecutionReportParser();
    sendTestExecutionReportEmail(testExecutionStatusParser.isAssertionFailureFound());    

}

public static void sendTestExecutionReportEmail(boolean testExecutionStatus) throws IOException {
    String mailSubject = null;
    String mailBody = null;     
    if (testExecutionStatus) {
        mailSubject = "Sub";
        mailBody = "body";
        sendEmail(mailSubject,mailBody);            
    }
    else {
        mailSubject = "Sub";
        mailBody = "body";          
        sendEmail(mailSubject,mailBody);
    }

}

public static void sendEmail(String mailSubject, String mailBody) throws IOException {
    Properties props = new Properties();
    Properties prop = new Properties();
    PropertyFileLoader propFileLoader = new PropertyFileLoader();
    prop = propFileLoader.loadPropertyFile("PropertyFiles", "EmailConfiguration.properties");
    props.put("mail.smtp.host", prop.getProperty("SMTPHostName"));
    props.put("mail.smtp.port", prop.getProperty("SMTPPort"));
    props.put("mail.smtp.ssl.trust", prop.getProperty("SMTPHostName"));
    final String EmailP = prop.getProperty("SMTPP");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.connectiontimeout", "10000");
    final String User = prop.getProperty("SMTPU");
    Configurations config = new Configurations();
    String reportFolderPath = "TestExecutionReports";
    String reportName = "report.html";

    try {
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(User, EmailP);
            }
        });

        InternetAddress fromAddress = new InternetAddress("email", "name");
        InternetAddress toAddress = new InternetAddress("email", "name");
        session.getProviders();
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        msg.setFrom(fromAddress);
        msg.addRecipient(Message.RecipientType.TO, toAddress);
        msg.setSubject(mailSubject);
        Multipart multipart = new MimeMultipart();
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(mailBody);
        MimeBodyPart attachmentBodyPart = new MimeBodyPart();
        String attachmentName = config.getResourcePath(reportFolderPath) + reportName;
        DataSource source = new FileDataSource(attachmentName);
        attachmentBodyPart.setDataHandler(new DataHandler(source));
        attachmentBodyPart.setFileName("Report.html");
        multipart.addBodyPart(textBodyPart);
        msg.setContent(multipart);      
        Transport.send(msg);

    } catch (MessagingException e) {
        System.out.println(e.getMessage() + e.getStackTrace());
    } catch (UnsupportedEncodingException e) {
        System.out.println(e.getMessage());
    }
}

代码中没有显示错误,但是当我运行它时,它会在控制台中抛出以下错误,

DEBUG: setDebug: JavaMail version 1.4.3
DEBUG: getProvider() returning 
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun 
Microsystems, Inc.,1.4.3]
java.lang.ClassNotFoundException: com.sun.mail.smtp.SMTPTransport
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

最初写代码的时候,虽然我有POM.xml文件中提到的最新版本的依赖,但是在Maven Dependencies下找不到任何Javax.mail jar文件,但是代码没有显示任何错误。后来我设法通过 Maven 正确安装了 Javax.mail 库(版本 1.6.1),但是当我运行测试时,上面的异常不断抛出,并且从上面控制台输出的第一行开始,我假设 Maven 是正在寻找旧版本的 Javax.mail 库,由于它不可用,所以它会抛出 classNotFound 异常。但我不确定如何将 Maven 指向 Maven 依赖项下可用的最新 Javax.mail 库。

非常感谢任何帮助...!

编辑:mvn 依赖:树日志如下,

 RestAssured:APITesting:jar:0.0.1-SNAPSHOT
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.0:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson- 
annotations:jar:2.9.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.0:compile
[INFO] +- io.rest-assured:rest-assured:jar:3.0.7:test
[INFO] |  +- org.codehaus.groovy:groovy:jar:2.4.12:test
[INFO] |  +- org.codehaus.groovy:groovy-xml:jar:2.4.12:test
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.3:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.6:compile
[INFO] |  |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.9:compile
[INFO] |  +- org.apache.httpcomponents:httpmime:jar:4.5.1:compile
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.ccil.cowan.tagsoup:tagsoup:jar:1.2.1:test
[INFO] |  +- io.rest-assured:json-path:jar:3.0.7:test
[INFO] |  |  +- org.codehaus.groovy:groovy-json:jar:2.4.12:test
[INFO] |  |  \- io.rest-assured:rest-assured-common:jar:3.0.7:test
[INFO] |  \- io.rest-assured:xml-path:jar:3.0.7:test
[INFO] |     \- javax.xml.bind:jaxb-api:jar:2.2.12:test
[INFO] +- junit:junit:jar:4.12:compile
[INFO] +- io.rest-assured:json-schema-validator:jar:3.0.3:test
[INFO] |  \- com.github.fge:json-schema-validator:jar:2.2.6:test
[INFO] |     +- com.google.code.findbugs:jsr305:jar:3.0.0:test
[INFO] |     +- joda-time:joda-time:jar:2.3:test
[INFO] |     +- com.googlecode.libphonenumber:libphonenumber:jar:6.2:test
[INFO] |     +- com.github.fge:json-schema-core:jar:1.2.5:test
[INFO] |     |  +- com.github.fge:uri-template:jar:0.9:test
[INFO] |     |  |  +- com.github.fge:msg-simple:jar:1.1:test
[INFO] |     |  |  |  \- com.github.fge:btf:jar:1.2:test
[INFO] |     |  |  \- com.google.guava:guava:jar:16.0.1:test
[INFO] |     |  +- com.github.fge:jackson-coreutils:jar:1.8:test
[INFO] |     |  \- org.mozilla:rhino:jar:1.7R4:test
[INFO] |     +- javax.mail:mailapi:jar:1.4.3:test
[INFO] |     \- net.sf.jopt-simple:jopt-simple:jar:4.6:test
[INFO] +- info.cukes:cucumber-java:jar:1.2.5:test
[INFO] |  \- info.cukes:cucumber-core:jar:1.2.5:test
[INFO] |     +- info.cukes:cucumber-html:jar:0.2.3:test
[INFO] |     +- info.cukes:cucumber-jvm-deps:jar:1.0.5:test
[INFO] |     \- info.cukes:gherkin:jar:2.12.2:test
[INFO] +- info.cukes:cucumber-junit:jar:1.2.5:test
[INFO] +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] +- org.assertj:assertj-core:jar:3.5.1:test
[INFO] +- com.vimalselvam:cucumber-extentsreport:jar:3.0.0:compile
[INFO] +- com.aventstack:extentreports:jar:3.0.6:compile
[INFO] |  +- org.freemarker:freemarker:jar:2.3.23:compile
[INFO] |  +- org.mongodb:mongodb-driver:jar:3.3.0:compile
[INFO] |  |  +- org.mongodb:bson:jar:3.3.0:compile
[INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:3.3.0:compile
[INFO] |  \- org.jsoup:jsoup:jar:1.9.2:compile
[INFO] +- com.github.javafaker:javafaker:jar:0.12:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] |  \- com.github.mifmif:generex:jar:1.0.2:compile
[INFO] |     \- dk.brics.automaton:automaton:jar:1.11-8:compile
[INFO] +- javax.mail:javax.mail-api:jar:1.6.1:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] \- log4j:log4j:jar:1.2.17:compile

【问题讨论】:

  • 发布mvn dependency:tree 日志。
  • 你的黄瓜 junit jar 版本是什么?
  • 在你的 pom.xml 依赖 jar 中,如果任何一个 jar 有这个 javax.mail 版本 1.4.3 作为依赖,那么你需要排除这个版本的 jar。
  • @user2249207 - 是的,有一个 javax.mail 工件作为另一个依赖项的一部分,而 lexicore 帮助我找到了它。问题现已解决。

标签: java maven jakarta-mail classnotfoundexception maven-dependency-plugin


【解决方案1】:

您需要从io.rest-assured:json-schema-validator 中排除javax.mail:mailapi

<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>json-schema-validator</artifactId>
  <version>3.0.3</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>javax.mail</groupId>
      <artifactId>mailapi</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

问题在于您的pom.xml 实际上包含两个Java Mail API 工件:javax.mail:mailapijavax.mail:javax.mail-api。从技术上讲,这是两个不同的工件,因此包括具有特定版本的 javax.mail-api 不会覆盖 mailapi 的版本。

无论如何mailapi 似乎是无效的工件,可能是某人错误部署的。

您可能还想联系json-schema-validator 的开发人员,让他们知道他们使用了无效的mailapi 工件。

【讨论】:

  • 我已经在json-schema-validator 中提交了issue
  • 非常感谢您的帮助,您的解决方案解决了我面临的问题,并且不再抛出此异常。再次感谢..! :)
  • 我已经试过up-voting,但是它说那些声望低于15的人的投票被记录了,但不要改变公众的看法。一旦我获得这 15 名声望,我肯定会投票赞成 :)
【解决方案2】:

在你空闲时,例如在 IntelliJ 的情况下。重新导入您的 Maven 项目依赖项。

您可以通过单击项目结构-> pom.xml > 右键单击​​ > Maven > 重新导入来实现此目的。

这应该重新导入所有依赖项。确保如您所说,在重新导入之前,您的 pom.xml 中提到的版本正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 2018-04-16
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2012-12-15
    相关资源
    最近更新 更多