c++mavenvisual-c++clionmaven-nar-plugin

Building C/C++ Maven NAR project from CLion terminal using Visual C++ compiler (msvc): Cannot deduce version number


I have a very simple C++ Maven NAR project and I want to compile it from CLion's terminal. This is my project's pom.xml:

<?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>

    <groupId>me.app.sample</groupId>
    <artifactId>app-sample</artifactId>
    <version>1.0</version>
    <packaging>nar</packaging>

    <build>
        <defaultGoal>integration-test</defaultGoal>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>3.0.0</version>
                <extensions>true</extensions>
                <configuration>
                    <libraries>
                        <library>
                            <type>executable</type>
                            <run>true</run>
                        </library>
                    </libraries>
                </configuration>
            </plugin>
        </plugins>
    </build>   
</project>

A simple main.cpp:

#include <iostream>
using namespace std;
int main() {
    cout << "Hello world" << endl;
    return 0;
}

And this directory structure:

directory structure C/C++ Maven NAR project

I can compile it if I use the "Developer Command Prompt".

[INFO] -----------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------

The problem arises when using CLion's terminal (ALT+F12) for building the project:

E:\...app-sample>mvn compile
...
[INFO] Using AOL: x86-Windows-msvc
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
...
[ERROR] Failed to execute goal com.github.maven-nar:nar-maven-plugin:3.0.0:nar-validate (default-nar-validate) on project app-sample: Cannot deduce version number from: -> [Help 1]
...

Solution

  • Cannot deduce version number from: ->

    NAR tries to make a system call to link.exe and/or cl.exe (if I recall correctly) with a flag like /version, so that it can determine the version of MSVS, so that it can pass arguments in the correct way for that version. This is necessary because different versions of MSVS are not backwards compatible when it comes to the command line syntax.

    However, if the executable in question is not found, the command spits out nothing on stdout (again, IIRC), producing an empty string for the version output, which cannot be parsed.

    It would certainly be nice to improve this error message to be more clear. In the meantime, you can try running with mvn -X to invoke debug mode, which will emit a lot more output from Maven, including a bunch more from NAR.

    Anyway, the crux of the issue is that the CLion CLI environment (i.e.: which variables are set to what) must differ from the CLI environment of your Developer Command Prompt. You could try using set to list variables in each, then diff the outputs.

    To fix this, you will probably need to either: A) tweak something in your CLion environment; or B) teach the NAR plugin about CLion and/or your MSVS configuration by patching it.

    If you go the route of (B), feel free to submit a PR! And either way, adding something about CLion to the NAR wiki would be great.