0

I'm working on a project where I need to run the maven install command in other projects. To resolve this, I used MavenCli which allows the execution of Maven commands by a Java Spring Boot project. A snippet of the executed code:

private int install(File pom, PrintStream out) throws IOException {
        log.info("Maven install started: " + pom.getCanonicalPath());
        System.setProperty("maven.multiModuleProjectDirectory", pom.getCanonicalPath());
        return this.maven.doMain(new String[]{
                        "install"},
                pom.getCanonicalPath(), out, out);
    }

The this.maven is initialized in the constructor with this.maven = new MavenCli(). The pom file have the path: "./src/main/resources/github-tasks/main/java-coding-tasks".

In localhost, the code works perfect, but when I deploy in Heroku and call this method, Heroku throw a NoSuchElementException:

2022-04-07T19:21:44.993728+00:00 app[web.1]: 2022-04-07 19:21:44.993  INFO 4 --- [onPool-worker-3] c.x.c.repository.MavenRepository         : 2022-04-07 19:21:44.574 ERROR 4 --- [onPool-worker-3] org.apache.maven.cli.MavenCli            : Error executing Maven.
2022-04-07T19:21:44.993735+00:00 app[web.1]: 2022-04-07 19:21:44.943 ERROR 4 --- [onPool-worker-3] org.apache.maven.cli.MavenCli            : java.util.NoSuchElementException
2022-04-07T19:21:44.993735+00:00 app[web.1]: role: org.apache.maven.eventspy.internal.EventSpyDispatcher
2022-04-07T19:21:44.993736+00:00 app[web.1]: roleHint:
2022-04-07T19:21:44.993737+00:00 app[web.1]: 2022-04-07 19:21:44.969 ERROR 4 --- [onPool-worker-3] org.apache.maven.cli.MavenCli            : Caused by: null

I already tried to change the pom file to relativePath, absolutePath, "./src/main/resources/github-tasks/main/java-coding-tasks/pom.xml", mixed the paths( in the system property "maven.multiModuleProjectDirectory" used relativePath and in the maven.doMain used absolutePath and vice versa), etc.

By the way, if I try to list the files in the directory that gives the NoSuchElementException, the files are there:

log.info("Directory in maven install: {}", Arrays.toString(pom.list()));

Log:

Directory in maven install: [pom.xml, .github, src, .git, README.md, .gitignore]

Does anyone know what's causing this and how to fix it?

  • Why calling Maven from another project? What kind of problem do you have? – khmarbaise Apr 07 '22 at 20:54
  • The purpose of this project is to check if other Java projects are right (correction of coding exercises). And the easiest and most practical way I found to do such a check would be through Maven install. Because I get a URL from a Github repository, based on that URL I clone the project and put the source code (only the 'src' folder) in another project that already contains all unit and integration tests for the project that received to check it, after that I run the maven install, because Maven itself will download the dependencies for me, compile and run tests. – Gustavo Steinmetz Apr 07 '22 at 21:23
  • If maven install does not find any problems in these steps, it means that the project has passed the verification, so I send a request to another component confirming that this project is correct. – Gustavo Steinmetz Apr 07 '22 at 21:24
  • I would suggest not to use `mvn install` better use `mvn verify` because you don't need to install all the files in your local cache...I also recommend to use ProcessBuilder and really run the `mvn` command instead of `maven.multiModuleProjectDirectory`? (not officiall)... – khmarbaise Apr 07 '22 at 21:28
  • It is a great idea to use `mvn verify`, thanks! But the ProcessBuilder has a problem to use, it because I don't have access to all environment variables of Heroku, for example, if I execute `mvn --version` in the Heroku console returns: `bash: mvn: command not found` – Gustavo Steinmetz Apr 07 '22 at 21:44

0 Answers0