Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #41 from jenkinsci/JENKINS-43714
[Jenkins 43714] fix withMaven thinks that "mvn test" generates a jar file causing a FileNotFoundException trying to archive it
  • Loading branch information
Cyrille Le Clerc committed Apr 20, 2017
2 parents 1b779fe + 1455ad2 commit 70cbc0a
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 6 deletions.
Expand Up @@ -79,7 +79,9 @@ public void processMavenSpyLogs(StepContext context, FilePath mavenSpyLogFolder)

for (FilePath mavenSpyLogs : mavenSpyLogsList) {
try {
LOGGER.log(Level.INFO, "Evaluate Maven Spy logs: " + mavenSpyLogs.getRemote());
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] Evaluate Maven Spy logs: " + mavenSpyLogs.getRemote());
}
InputStream mavenSpyLogsInputStream = mavenSpyLogs.read();
if (mavenSpyLogsInputStream == null) {
throw new IllegalStateException("InputStream for " + mavenSpyLogs.getRemote() + " is null");
Expand Down
Expand Up @@ -63,6 +63,14 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
listener.getLogger().println("[withMaven] Can't archive maven artifact with no file attached: " + mavenArtifact);
}
continue;
} else if (!(mavenArtifact.file.endsWith("." + mavenArtifact.extension))) {
FilePath mavenGeneratedArtifact = workspace.child(XmlUtils.getPathInWorkspace(mavenArtifact.file, workspace));
if (mavenGeneratedArtifact.isDirectory()) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] Skip archiving for generated maven artifact of type directory (it's likely to be target/classes, see JENKINS-43714) " + mavenArtifact);
}
continue;
}
}

String artifactPathInArchiveZone =
Expand Down
Expand Up @@ -129,7 +129,7 @@ public void maven_build_on_master_with_missing_specified_maven_installation_fail
}

@Test
public void maven_build_on_master_succeeds() throws Exception {
public void maven_build_jar_project_on_master_succeeds() throws Exception {
loadMavenJarProjectInGitRepo(this.gitRepoRule);

String pipelineScript = "node('master') {\n" +
Expand Down Expand Up @@ -173,6 +173,106 @@ public void maven_build_on_master_succeeds() throws Exception {
assertThat(tasksResultAction.getProjectActions().size(), is(1));
}

@Test
public void maven_build_maven_hpi_project_on_master_succeeds() throws Exception {
loadJenkinsPluginProjectInGitRepo(this.gitRepoRule);

String pipelineScript = "node('master') {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn package'\n" +
" }\n" +
"}";

WorkflowJob pipeline = jenkinsRule.createProject(WorkflowJob.class, "build-on-master");
pipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, pipeline.scheduleBuild2(0));

// verify Maven installation provided by the build agent is used
// can be either "by the build agent with executable..." or "by the build agent with the environment variable MAVEN_HOME=..."
jenkinsRule.assertLogContains("[withMaven] use Maven installation provided by the build agent with", build);

// verify .pom is archived and fingerprinted
jenkinsRule.assertLogContains("under jenkins/mvn/test/test-jenkins-hpi/0.1-SNAPSHOT/test-jenkins-hpi-0.1-SNAPSHOT.pom", build);

// verify .jar and .hpi is archived and fingerprinted
jenkinsRule.assertLogContains("under jenkins/mvn/test/test-jenkins-hpi/0.1-SNAPSHOT/test-jenkins-hpi-0.1-SNAPSHOT.hpi", build);
jenkinsRule.assertLogContains("under jenkins/mvn/test/test-jenkins-hpi/0.1-SNAPSHOT/test-jenkins-hpi-0.1-SNAPSHOT.jar", build);

System.out.println(getClass() + " artifacts: " + build.getArtifacts());
// TODO build.getArtifacts() is always empty (size=0), skip verification

verifyFileIsFingerPrinted(pipeline, build, "jenkins/mvn/test/test-jenkins-hpi/0.1-SNAPSHOT/test-jenkins-hpi-0.1-SNAPSHOT.hpi");
verifyFileIsFingerPrinted(pipeline, build, "jenkins/mvn/test/test-jenkins-hpi/0.1-SNAPSHOT/test-jenkins-hpi-0.1-SNAPSHOT.jar");
verifyFileIsFingerPrinted(pipeline, build, "jenkins/mvn/test/test-jenkins-hpi/0.1-SNAPSHOT/test-jenkins-hpi-0.1-SNAPSHOT.pom");

// verify Junit Archiver is called for jenkins.mvn.test:test-jenkins-hpi
jenkinsRule.assertLogContains("[withMaven] Archive test results for Maven artifact MavenArtifact{jenkins.mvn.test:test-jenkins-hpi::0.1-SNAPSHOT} generated by", build);

// verify Task Scanner is called for jenkins.mvn.test:test-jenkins-hpi
jenkinsRule.assertLogContains("[withMaven] Scan Tasks for Maven artifact MavenArtifact{jenkins.mvn.test:test-jenkins-hpi::0.1-SNAPSHOT} in source directory", build);
}

@Test
public void maven_build_maven_plugin_project_on_master_succeeds() throws Exception {
loadMavenPluginProjectInGitRepo(this.gitRepoRule);

String pipelineScript = "node('master') {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn package'\n" +
" }\n" +
"}";

WorkflowJob pipeline = jenkinsRule.createProject(WorkflowJob.class, "build-on-master");
pipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, pipeline.scheduleBuild2(0));

// verify Maven installation provided by the build agent is used
// can be either "by the build agent with executable..." or "by the build agent with the environment variable MAVEN_HOME=..."
jenkinsRule.assertLogContains("[withMaven] use Maven installation provided by the build agent with", build);

// verify .pom is archived and fingerprinted
jenkinsRule.assertLogContains("under jenkins/mvn/test/maven-test-plugin/1.0-SNAPSHOT/maven-test-plugin-1.0-SNAPSHOT.pom", build);

// verify .jar and .hpi is archived and fingerprinted
jenkinsRule.assertLogContains("under jenkins/mvn/test/maven-test-plugin/1.0-SNAPSHOT/maven-test-plugin-1.0-SNAPSHOT.jar", build);

System.out.println(getClass() + " artifacts: " + build.getArtifacts());
// TODO build.getArtifacts() is always empty (size=0), skip verification

verifyFileIsFingerPrinted(pipeline, build, "jenkins/mvn/test/maven-test-plugin/1.0-SNAPSHOT/maven-test-plugin-1.0-SNAPSHOT.jar");
verifyFileIsFingerPrinted(pipeline, build, "jenkins/mvn/test/maven-test-plugin/1.0-SNAPSHOT/maven-test-plugin-1.0-SNAPSHOT.pom");

// verify Junit Archiver is called for jenkins.mvn.test:test-jenkins-hpi
jenkinsRule.assertLogContains("[withMaven] Archive test results for Maven artifact MavenArtifact{jenkins.mvn.test:maven-test-plugin::1.0-SNAPSHOT} generated by", build);

// verify Task Scanner is called for jenkins.mvn.test:test-jenkins-hpi
jenkinsRule.assertLogContains("[withMaven] Scan Tasks for Maven artifact MavenArtifact{jenkins.mvn.test:maven-test-plugin::1.0-SNAPSHOT} in source directory", build);
}

/**
* JENKINS-43678
*/
@Test
public void maven_build_on_master_with_no_generated_jar_succeeds() throws Exception {
loadMavenJarProjectInGitRepo(this.gitRepoRule);

String pipelineScript = "node('master') {\n" +
" git($/" + gitRepoRule.toString() + "/$)\n" +
" withMaven() {\n" +
" sh 'mvn test'\n" +
" }\n" +
"}";

WorkflowJob pipeline = jenkinsRule.createProject(WorkflowJob.class, "build-on-master");
pipeline.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun build = jenkinsRule.assertBuildStatus(Result.SUCCESS, pipeline.scheduleBuild2(0));

// don't try to archive the artifact as it has not been generated
jenkinsRule.assertLogNotContains("under jenkins/mvn/test/mono-module-maven-app/0.1-SNAPSHOT/mono-module-maven-app-0.1-SNAPSHOT.jar", build);
}

private void verifyFileIsFingerPrinted(WorkflowJob pipeline, WorkflowRun build, String fileName) throws java.io.IOException {
System.out.println(getClass() + " verifyFileIsFingerPrinted(" + build + ", " + fileName + ")");
Fingerprinter.FingerprintAction fingerprintAction = build.getAction(Fingerprinter.FingerprintAction.class);
Expand Down Expand Up @@ -225,14 +325,24 @@ public void mavenSettingsFilePath_should_work_with_relative_path() throws Except
}

private void loadMavenJarProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
gitRepo.init();
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_project/");
}

private void loadMavenPluginProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
loadSourceCodeInGitRepository(gitRepo, "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_plugin_project/");
}

// File mavenProjectRoot = new File("src/test/test-maven-projects/maven-jar-project");
Path mavenProjectRoot = Paths.get(WithMavenStepOnMasterTest.class.getResource("/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_jar_project/").toURI());
private void loadJenkinsPluginProjectInGitRepo(GitSampleRepoRule gitRepo) throws Exception {
String name = "/org/jenkinsci/plugins/pipeline/maven/test/test_maven_projects/maven_hpi_project/";
loadSourceCodeInGitRepository(gitRepo, name);
}

private void loadSourceCodeInGitRepository(GitSampleRepoRule gitRepo, String name) throws Exception {
gitRepo.init();
Path mavenProjectRoot = Paths.get(WithMavenStepOnMasterTest.class.getResource(name).toURI());
if (!Files.exists(mavenProjectRoot)) {
throw new IllegalStateException("Folder '" + mavenProjectRoot + "' not found");
}

GitSampleRepoRuleUtils.addFilesAndCommit(mavenProjectRoot, this.gitRepoRule);
}
}
@@ -0,0 +1,4 @@
target/
work/

*.log
@@ -0,0 +1,40 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.26</version>
<relativePath />
</parent>

<groupId>jenkins.mvn.test</groupId>
<artifactId>test-jenkins-hpi</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>hpi</packaging>
<properties>
<!-- make it match with the version used by the pipeline-maven-plugin to faster the downloads -->
<jenkins.version>2.7.3</jenkins.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>

@@ -0,0 +1,11 @@
package com.example;

public class MonoModuleMavenApp {
/**
* TODO message
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello MonoModuleMavenApp");
}
}
@@ -0,0 +1,11 @@
package com.example;

import org.junit.Test;

public class MonoModuleMavenAppTest {

@Test
public void success(){
System.out.println("MonoModuleMavenAppTest#success");
}
}
@@ -1 +1,4 @@
target/
work/

*.log
Expand Up @@ -9,6 +9,7 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Down
@@ -0,0 +1,4 @@
target/
work/

*.log
@@ -0,0 +1,24 @@
<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>jenkins.mvn.test</groupId>
<artifactId>maven-test-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>maven-test-plugin</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,13 @@
package jenkins.mvn.test;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
@@ -0,0 +1,38 @@
package jenkins.mvn.test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

0 comments on commit 70cbc0a

Please sign in to comment.