Unable to execute mvn command with -pl (--projects) options


(Plum117) #1

Hi,

Using go execute command I tried to run something like ‘mvn -pl test:test compile’. However, the go exec return an error ’ Unable to parse command line options: Unrecognized option: -pl test:test compile’.

Note that if I run the command on command line it work perfectly. I have the same problem with --projects.

Below, is a simple project I setup to demonstrate my problem. The command is mvn --version, the second the one in problem, and when running it using the bash as the command, then it is executed seamlessly.

Any idea why my second command is returning an error?

Go code:

package main

import (
	"os/exec"
	"fmt"
	"strings"
	"bytes"
)

func main() {


	//Calling mvn using bash since without it, we have an syntax error when using -pl (--projects) options
	cmd := exec.Command("mvn", "--version")
	//cmd := exec.Command("mvn", "compile")
	funcName(cmd)

	cmd2 := exec.Command("mvn", "-pl test:test compile")
	//cmd := exec.Command("mvn", "compile")
	funcName(cmd2)

	cmd3 := exec.Command("bash", "-c", "mvn -pl test:test compile")

	funcName(cmd3)
}

func funcName(cmd *exec.Cmd) {
	cmd.Dir = "test"

	fmt.Println("--------------------------------------------------------------------------------------", )
	fmt.Println("Executing:", "'"+strings.Join(cmd.Args, " ")+"'", "With working directory:", cmd.Dir)
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
	fmt.Printf("Command fail with: %s", errStr)
	if err != nil {
		fmt.Printf("Command fail with: %s\n", err)
	}
	fmt.Printf("Execution log: %s", outStr)
	fmt.Println("--------------------------------------------------------------------------------------", )
}

output:

Executing: 'mvn --version' With working directory: test
Command fail with: Execution log: Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T14:49:05-05:00)
Maven home: /home/dprevost/mfxdev-snapshot/software/apache-maven-3.5.3
Java version: 1.8.0_92, vendor: Oracle Corporation
Java home: /home/dprevost/mfxdev-snapshot/software/jdk1.8.0_92/jre
Default locale: en_CA, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-22-generic", arch: "amd64", family: "unix"
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
Executing: 'mvn -pl test:test compile' With working directory: test
Command fail with: Unable to parse command line options: Unrecognized option: -pl test:test compile
Command fail with: exit status 1
Execution log: 
usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -am,--also-make                        If project list is specified, also
                                        build projects required by the
                                        list
 -amd,--also-make-dependents            If project list is specified, also
                                        build projects that depend on
                                        projects on the list
 -B,--batch-mode                        Run in non-interactive (batch)
                                        mode (disables output color)
 -b,--builder <arg>                     The id of the build strategy to
                                        use
 -C,--strict-checksums                  Fail the build if checksums don't
                                        match
 -c,--lax-checksums                     Warn if checksums don't match
 -cpu,--check-plugin-updates            Ineffective, only kept for
                                        backward compatibility
 -D,--define <arg>                      Define a system property
 -e,--errors                            Produce execution error messages
 -emp,--encrypt-master-password <arg>   Encrypt master security password
 -ep,--encrypt-password <arg>           Encrypt server password
 -f,--file <arg>                        Force the use of an alternate POM
                                        file (or directory with pom.xml)
 -fae,--fail-at-end                     Only fail the build afterwards;
                                        allow all non-impacted builds to
                                        continue
 -ff,--fail-fast                        Stop at first failure in
                                        reactorized builds
 -fn,--fail-never                       NEVER fail the build, regardless
                                        of project result
 -gs,--global-settings <arg>            Alternate path for the global
                                        settings file
 -gt,--global-toolchains <arg>          Alternate path for the global
                                        toolchains file
 -h,--help                              Display help information
 -l,--log-file <arg>                    Log file where all build output
                                        will go (disables output color)
 -llr,--legacy-local-repository         Use Maven 2 Legacy Local
                                        Repository behaviour, ie no use of
                                        _remote.repositories. Can also be
                                        activated by using
                                        -Dmaven.legacyLocalRepo=true
 -N,--non-recursive                     Do not recurse into sub-projects
 -npr,--no-plugin-registry              Ineffective, only kept for
                                        backward compatibility
 -npu,--no-plugin-updates               Ineffective, only kept for
                                        backward compatibility
 -nsu,--no-snapshot-updates             Suppress SNAPSHOT updates
 -o,--offline                           Work offline
 -P,--activate-profiles <arg>           Comma-delimited list of profiles
                                        to activate
 -pl,--projects <arg>                   Comma-delimited list of specified
                                        reactor projects to build instead
                                        of all projects. A project can be
                                        specified by [groupId]:artifactId
                                        or by its relative path
 -q,--quiet                             Quiet output - only show errors
 -rf,--resume-from <arg>                Resume reactor from specified
                                        project
 -s,--settings <arg>                    Alternate path for the user
                                        settings file
 -t,--toolchains <arg>                  Alternate path for the user
                                        toolchains file
 -T,--threads <arg>                     Thread count, for instance 2.0C
                                        where C is core multiplied
 -U,--update-snapshots                  Forces a check for missing
                                        releases and updated snapshots on
                                        remote repositories
 -up,--update-plugins                   Ineffective, only kept for
                                        backward compatibility
 -v,--version                           Display version information
 -V,--show-version                      Display version information
                                        WITHOUT stopping build
 -X,--debug                             Produce execution debug output
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
Executing: 'bash -c mvn -pl test:test compile' With working directory: test
Command fail with: Execution log: [INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.560 s
[INFO] Finished at: 2018-06-12T11:16:10-04:00
[INFO] ------------------------------------------------------------------------
--------------------------------------------------------------------------------------

(Norbert Melzer) #2

Those arguments need to be 3 separate strings, eg. exec.Command("mvn", "-pl", "test:test", "compile") the way you tried it were similar to mvn "-pl test:test compile" in your shell.


(Plum117) #3

Thanks, it works now. By the way, do you know if there is a way to output exactly the command that go is running on the shell?


(Norbert Melzer) #4

That’s exactly it… Nothing is run on a shell.

The form you need to use in go, is the form a shell would parse it to before making a similar call in its implementing language.


(system) #5

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.