Portable Maven launch

11 views
Skip to first unread message

Jochen Wiedmann

unread,
Jan 23, 2021, 3:19:43 PM1/23/21
to Jenkins Users

Hi,

I've got a pipeline file, that should be executable on a Windows build server, and on a Linux build server. The pipeline is launching Maven as follows:

On Linux:

    sh "mvn <MAVEN_OPTIONS>"

But on Windows, this appears not to work, so I've got to use:

   bat "mvn.cmd <MAVEN_OPTIONS>" 

Right now, I can use either of these steps, so have to choose between Windows, and Linux. Is there any possibility to get this portable? I am thinking something like

   if (isWindows()) {
       bat "mvn.cmd <MAVEN_OPTIONS>" 
   } else {
    sh "mvn <MAVEN_OPTIONS>"
   }

Thanks,

Jochen



geoffroy...@gmail.com

unread,
Jan 24, 2021, 11:54:26 AM1/24/21
to Jenkins Users

Jochen Wiedmann

unread,
Jan 24, 2021, 3:30:54 PM1/24/21
to Jenkins Users
Thanks,

sounds like what I was looking for. But then: Any ideas, what's wrong in my script?

pipeline {
    agent any
    tools { 
        maven 'Maven3' 
        jdk 'Java8' 
    }
    stages {
        stage ('afw-core') {
            steps {
                withMaven(
                     // Maven installation declared in the Jenkins "Global Tool Configuration"
                     maven: 'Maven3',

                    // Use `$WORKSPACE/.repository` for local repository folder to avoid shared repositories
                    mavenLocalRepo: '.repository',
                ) {
    if (isUnix()) {
            sh 'mvn -fafw/afw-core/pom.xml -Pjacoco -Dmaven.test.failure.ignore=true clean install'
} else {
            bat 'mvn.cmd -fafw/afw-core/pom.xml -Pjacoco -Dmaven.test.failure.ignore=true clean install'
}
                }
            }
        }
        stage ('afw-bootstrap') {
            steps {
                withMaven(
                     // Maven installation declared in the Jenkins "Global Tool Configuration"
                     maven: 'Maven3',

                    // Use `$WORKSPACE/.repository` for local repository folder to avoid shared repositories
                    mavenLocalRepo: '.repository',
                ) {
    if (isUnix()) {
            sh 'mvn -fafw/afw-bootstrap/pom.xml -Pjacoco -Dmaven.test.failure.ignore=true clean install'
} else {
            bat 'mvn.cmd -fafw/afw-bootstrap/pom.xml -Pjacoco -Dmaven.test.failure.ignore=true clean install'
}
                }
            }
        }
    }
}

The error message I am getting:


org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 17: Expected a step @ line 17, column 9.
                                    if (isUnix()) {
           ^
WorkflowScript: 34: Expected a step @ line 34, column 9.
                                    if (isUnix()) {
           ^

Martin Schmude

unread,
Jan 25, 2021, 11:05:05 AM1/25/21
to Jenkins Users
The if statement is not a pipeline step. It has to be wrapped in a script block: 
Reply all
Reply to author
Forward
0 new messages