1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Fix installing maven artefacts in spring projects using MTA (#1892)

Currently, the mtaBuild step installs the wrong artifact in a spring project making use of the "repackage" feature. This PR fixes that by checking if an ".original" jar file exists and using that instead.
This commit is contained in:
Florian Wilhelm
2020-08-06 15:12:21 +02:00
committed by GitHub
parent 896519a77f
commit c887231234
13 changed files with 215 additions and 5 deletions

View File

@@ -35,6 +35,33 @@ func TestMavenProject(t *testing.T) {
container.assertHasOutput(t, "added 2 packages from 3 contributors and audited 2 packages in")
}
func TestMavenSpringProject(t *testing.T) {
t.Parallel()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: "maven:3-openjdk-8-slim",
User: "root",
TestDir: []string{"testdata", "TestMtaIntegration", "maven-spring"},
Mounts: map[string]string{},
Setup: []string{
"apt-get -yqq update; apt-get -yqq install make",
"curl -OL https://github.com/SAP/cloud-mta-build-tool/releases/download/v1.0.14/cloud-mta-build-tool_1.0.14_Linux_amd64.tar.gz",
"tar xzf cloud-mta-build-tool_1.0.14_Linux_amd64.tar.gz",
"mv mbt /usr/bin",
},
})
err := container.whenRunningPiperCommand("mtaBuild", "--installArtifacts", "--m2Path=mym2")
if err != nil {
t.Fatalf("Piper command failed %s", err)
}
err = container.whenRunningPiperCommand("mavenExecuteIntegration", "--m2Path=mym2")
if err != nil {
t.Fatalf("Piper command failed %s", err)
}
container.assertHasOutput(t, "Tests run: 1, Failures: 0, Errors: 0, Skipped: 0")
}
func TestNPMProject(t *testing.T) {
t.Parallel()
container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{

View File

@@ -0,0 +1,10 @@
app/target
app/.pipeline
mtaBuild_errorDetails.json
foo.mtar
.flattened-pom.xml
.pipeline/commonPipelineEnvironment
mym2
app/node_modules
app/package-lock.json
integration-tests/target

View File

@@ -0,0 +1,3 @@
steps:
mtaBuild:
installArtifacts: true

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>mygroup</groupId>
<artifactId>mymvn</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>mygroup</groupId>
<artifactId>mymvn-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>

View File

@@ -0,0 +1,13 @@
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>mygroup</groupId>
<artifactId>mymvn</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>mygroup</groupId>
<artifactId>mymvn-integration-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>mymvn-app</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}

View File

@@ -0,0 +1,11 @@
_schema-version: "3.1"
ID: foo
version: 0.0.1
modules:
- name: foo
type: java
path: app
build-parameters:
builder: maven
build-result: target/*.jar

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>mygroup</groupId>
<artifactId>mymvn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>app</module>
<module>integration-tests</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,3 +0,0 @@
FROM devxci/mbtci:1.0.14
COPY run-in-container.sh /test.sh

View File

@@ -223,12 +223,20 @@ func installJarWarArtifacts(pomFile, dir string, command mavenExecRunner, utils
jarExists, _ := utils.FileExists(jarFile(dir, finalName))
warExists, _ := utils.FileExists(warFile(dir, finalName))
classesJarExists, _ := utils.FileExists(classesJarFile(dir, finalName))
originalJarExists, _ := utils.FileExists(originalJarFile(dir, finalName))
log.Entry().Infof("JAR file with name %s does exist: %t", jarFile(dir, finalName), jarExists)
log.Entry().Infof("WAR file with name %s does exist: %t", warFile(dir, finalName), warExists)
log.Entry().Infof("Classes-JAR file with name %s does exist: %t", classesJarFile(dir, finalName), classesJarExists)
log.Entry().Infof("Original-JAR file with name %s does exist: %t", originalJarFile(dir, finalName), originalJarExists)
log.Entry().Infof("WAR file with name %s does exist: %t", warFile(dir, finalName), warExists)
if jarExists {
// Due to spring's jar repackaging we need to check for an "original" jar file because the repackaged one is no suitable source for dependent maven modules
if originalJarExists {
err = InstallFile(originalJarFile(dir, finalName), pomFile, options.M2Path, command)
if err != nil {
return err
}
} else if jarExists {
err = InstallFile(jarFile(dir, finalName), pomFile, options.M2Path, command)
if err != nil {
return err
@@ -259,6 +267,10 @@ func classesJarFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+"-classes.jar")
}
func originalJarFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+".jar.original")
}
func warFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+".war")
}

View File

@@ -210,4 +210,24 @@ func TestMavenInstall(t *testing.T) {
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"-Dfile=" + filepath.Join(".", "target", "foo.war"), "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, execMockRunner.Calls[4])
}
})
t.Run("Install files in a spring-boot project", func(t *testing.T) {
utils := newMockUtils(false)
utils.AddFile("target/foo.jar", []byte("dummyContent"))
utils.AddFile("target/foo.jar.original", []byte("dummyContent"))
utils.AddFile("pom.xml", []byte("<project></project>"))
options := EvaluateOptions{}
execMockRunner := mock.ExecMockRunner{}
execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.build.finalName -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "foo"}
err := doInstallMavenArtifacts(&execMockRunner, options, &utils)
assert.NoError(t, err)
if assert.Equal(t, 4, len(execMockRunner.Calls)) {
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dflatten.mode=resolveCiFriendliesOnly", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "flatten:flatten"}}, execMockRunner.Calls[0])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dexpression=project.packaging", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, execMockRunner.Calls[1])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dexpression=project.build.finalName", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, execMockRunner.Calls[2])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"-Dfile=" + filepath.Join(".", "target", "foo.jar.original"), "-Dpackaging=jar", "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, execMockRunner.Calls[3])
}
})
}