无法在 Teamcity 构建后获取构件文件

huangapple 未分类评论44阅读模式
英文:

Unable to fetch Artifacts files after Teamcity builds

问题

以下是您的代码的翻译部分:

我有一个Java-Maven脚本用于从Teamcity上最后一次成功构建的工件文件发送邮件但每次构建完成并运行脚本时都会出现以下文件未找到错误是否有人可以帮助解决这个问题

错误信息:
java.io.FileNotFoundException: /repository/download/94ecc0rt7op01pew/.lastSuccessful/target/project-report/project-report.html文件或目录不存在

代码片段

private static final String SMTP_SERVER = "projectServer.net";
private static final String USERNAME = "user1";
private static final String PASSWORD = "user1";

private static final String EMAIL_FROM = "user1@.com";
private static final String EMAIL_TO = "user1@.com";
private static final String EMAIL_TO_CC = "user1@.com";

private static final String EMAIL_SUBJECT = "截止至 ";
private static final String EMAIL_TEXT = "请查看附加的状态报告。";

public static void sendReport() {

    Properties prop = System.getProperties();
    prop.put("mail.smtp.auth", "false");

    Session session = Session.getInstance(prop, null);
    Message msg = new MimeMessage(session);

    try {

        String today = new SimpleDateFormat("dd-MMM-yyyy").format(new Date(new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(new Date().toString()).getTime()));
        msg.setFrom(new InternetAddress(EMAIL_FROM));

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(EMAIL_TO, false));

        msg.setSubject(EMAIL_SUBJECT + today);

        // 文本内容
        MimeBodyPart p1 = new MimeBodyPart();
        p1.setText(EMAIL_TEXT);

        String cwd = new File("target").getAbsolutePath();

        System.out.println("当前工作目录: " + cwd);

        String BUILD_TYPE_EXT_ID = cwd.split("/")[5]; /* 获取构建ID */

        // 文件附件
        MimeBodyPart p2 = new MimeBodyPart();
        FileDataSource fds = new FileDataSource("/repository/download/" + BUILD_TYPE_EXT_ID + "/.lastSuccessful/target/project-report/project-report.html");
        p2.setDataHandler(new DataHandler(fds));
        p2.setFileName(fds.getName());

        Multipart mp = new MimeMultipart();
        mp.addBodyPart(p1);
        mp.addBodyPart(p2);

        msg.setContent(mp);

        SMTPTransport t = (SMTPTransport) session.getTransport("smtp");

        // 连接
        t.connect(SMTP_SERVER, USERNAME, PASSWORD);

        // 发送
        t.sendMessage(msg, msg.getAllRecipients());

        System.out.println("响应: " + t.getLastServerResponse());

        t.close();

    }

希望这可以帮助您解决问题。如果您需要进一步的帮助,请随时提问。

英文:

I have a Java-Maven script that sends the mail of an artifact file from last successful build on Teamcity. But every time the build completes and the scripts run, below File not found error occur. Can anyone please help on this.

Error:
java.io.FileNotFoundException: /repository/download/94ecc0rt7op01pew/.lastSuccessful/target/project-report/project-report.html (No such file or directory)

Code Snippet

private static final String SMTP_SERVER = "projectServer.net";
private static final String USERNAME = "user1";
private static final String PASSWORD = "user1";

private static final String EMAIL_FROM = "user1@.com";
private static final String EMAIL_TO = "user1@.com";
private static final String EMAIL_TO_CC = "user1@.com";

private static final String EMAIL_SUBJECT = "Services Status Report as of ";
private static final String EMAIL_TEXT = "Please find the attached status report.";

public static void sendReport() {

    Properties prop = System.getProperties();
    prop.put("mail.smtp.auth", "false");

    Session session = Session.getInstance(prop, null);
    Message msg = new MimeMessage(session);

    try {
    	
    	String today =	new SimpleDateFormat("dd-MMM-yyyy").format( new Date(new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(new Date().toString()).getTime()));
        msg.setFrom(new InternetAddress(EMAIL_FROM));

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(EMAIL_TO, false));

        msg.setSubject(EMAIL_SUBJECT+today);

        // text-body
        MimeBodyPart p1 = new MimeBodyPart();
        p1.setText(EMAIL_TEXT);

        String cwd = new File("target").getAbsolutePath();
    	
    	System.out.println("PWD: "+cwd);
    	String BUILD_TYPE_EXT_ID = cwd.split("/")[5]; /* To get the build id */
		
        // file-attachment
        MimeBodyPart p2 = new MimeBodyPart();            
        FileDataSource fds = new FileDataSource("/repository/download/"+BUILD_TYPE_EXT_ID+"/.lastSuccessful/target/project-report/project-report.html");
        p2.setDataHandler(new DataHandler(fds));
        p2.setFileName(fds.getName());

        Multipart mp = new MimeMultipart();
        mp.addBodyPart(p1);
        mp.addBodyPart(p2);

        msg.setContent(mp);


		SMTPTransport t = (SMTPTransport) session.getTransport("smtp");

		// connect
        t.connect(SMTP_SERVER, USERNAME, PASSWORD);

		// send
        t.sendMessage(msg, msg.getAllRecipients());

        System.out.println("Response: " + t.getLastServerResponse());

        t.close();

    

}

huangapple
  • 本文由 发表于 2020年7月27日 14:48:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/63110038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定