native-maven-plugin error with msvc compiler “The

2019-08-15 07:43发布

问题:

I'm trying to build a cpp lib via maven using native-mvn-plugin. However, during the linking part, i'm encountering an error saying "The command line is too long"

As for the configuration, im having this:

<envFactoryName>org.codehaus.mojo.natives.msvc.MSVC2008x86EnvFactory</envFactoryName>
<compilerProvider>msvc</compilerProvider>
<compilerStartOptions>
    <compilerStartOption> /GL /EHsc </compilerStartOption>
</compilerStartOptions>

And for the linkerStartOptions, i have this:

<linkerStartOptions>
    <linkerStartOption>-g -Fo -lstdc</linkerStartOption>
</linkerStartOptions>

Would be glad if somebody can help me.

回答1:

I would really discourage the use of the maven native plugin, I had a lot of trouble configuring it, and I don't know if it is maintained as the main page says it was last published on 2011-03-09. What I did to handle the problem of building a C++ library with maven was to use the maven-exec plugin. I load the msbuild tool by calling:

"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"

from the command line. After that, msbuild will be available in your scope.

These are the contents of my pom file :

<plugin>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
        <executable>msbuild</executable>
        <sourceRoot>${basedir}/Library/</sourceRoot>
    </configuration>
    <executions>
        <execution>
            <id>clean</id>
            <phase>clean</phase>
            <configuration>
                <arguments>
                    <argument>${basedir}/Library/Library.vcxproj</argument>
                    <argument>/p:Configuration=Release</argument>
                    <argument>/p:Platform=x64</argument>
                    <argument>/t:Clean</argument>
                </arguments>
            </configuration>
            <goals>
                <goal>exec</goal>
                </goals>
            </execution>
        <execution>
            <id>build</id>
            <phase>compile</phase>
            <configuration>
                <arguments>
                    <argument>${basedir}/Library/Library.vcxproj</argument>
                    <argument>/p:Configuration=Release</argument>
                    <argument>/p:Platform=x64</argument>
                    <argument>/t:Build</argument>
                </arguments>
            </configuration>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
</plugin>

That way the configuration will make that the project responds to both clean and compile goals. You can go even further and use the assembly plugin to pack your library and make it install the library in the local repository so it can be added as a dependency in other projects.