Search code examples
mavenmingwintegrationlifecycleeclipse-cdt

Using Maven to build c++ in eclipse


My Project consists of multiple Java-Projects, one Java-JNI-C++ Project as a Bridge and one pure C++ Project keeping an algorithm library. I managed to write Maven build configurations for all 3 project kinds. So when I'm calling them on the commandline (Windows 7, 64bit) everything works great.

I do not use any make files or something like this. I use exec-maven-plugin to call my mingw 64bit installation without cygwin (and I also didn't at least knowingly install msys). So 2 pure commandline g++ commands each for the JNA and Library Project.

What I need now for a smooth development workflow is to be able to build and debug the this projects from within Eclipse but using the maven build scripts, since I don't want to put work into my poms and additionally configure the eclipse builder. This should be consistent! Furthermore should the error parsing in Eclipse be konsistent with the output of the maven build.

For my Java projects this works greatly out of the box. Eclipse picks up the maven config and CLEAN and BUILD produces exactly what it should. (Though I see that the Java Builder is still active in the project's properties. Why??). But I cannot get it to work with the CDT.

When I disable the C++ Builder Eclipse just builds with maven (what I want), but the clean commands do not work correctly. Also I get errors marked which are not errors by the compiler. Of course this should be consistent.

Are there tutorials for this use case?

I did not find information on that subject. I'm not sure if I'm generally going into a wrong direction missing best practices or something?!

Since this is my first s.o. question, please feel free to give me also feedback on my question. What I can provide I will ;-)

Some Information:

System Windows 7, 64bit

Eclipse Juno, m2e

Library POM:

<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>test</groupId>
  <artifactId>mylib</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>MyLib</name>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <versionRange>[1.1.1,)</versionRange>
                    <goals>
                      <goal>exec</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute>
                      <runOnIncremental>true</runOnIncremental>
                    </execute>
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1.1</version>
        <executions>
          <execution>
            <id>compile-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target/cpp/Windows_x64</workingDirectory>
              <arguments>
                <argument>-Wall</argument>
                <argument>-m64</argument>
                <argument>-c</argument>
                <argument>-DAPI_EXPORT</argument>
                <argument>-g3</argument>
                <argument>-std=c++0x</argument>
                <argument>../../../src/main/cpp/*.cpp</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>link-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target</workingDirectory>
              <arguments>
                <argument>-shared</argument>
                <argument>-s</argument>
                <argument>-m64</argument>
                <argument>-oMyLib_Windows_x64.dll</argument>
                <argument>cpp/Windows_x64/*.o</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>none</phase>
          </execution>
        </executions>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

JNI POM:

<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>test</groupId>
  <artifactId>myprog</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>MyProg</name>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>truezip-maven-plugin</artifactId>
                    <versionRange>[1.1,)</versionRange>
                    <goals>
                      <goal>copy</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute>
                      <runOnIncremental>true</runOnIncremental>
                    </execute>
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <versionRange>[1.1.1,)</versionRange>
                    <goals>
                      <goal>exec</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute>
                      <runOnIncremental>true</runOnIncremental>
                    </execute>
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>truezip-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>get-library-headers</id>
            <goals>
              <goal>copy</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <fileset>
                <directory>../MyLib/target/mylib-0.0.1-SNAPSHOT-dll.zip</directory>
                <includes>
                  <include>headers/*</include>
                </includes>
                <outputDirectory>${project.build.directory}/myLib</outputDirectory>
              </fileset>
            </configuration>
          </execution>
          <execution>
            <id>get-library</id>
            <goals>
              <goal>copy</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <fileset>
                <directory>../MyLib/target/mylib-0.0.1-SNAPSHOT-dll.zip</directory>
                <includes>
                  <include>*.dll</include>
                </includes>
                <outputDirectory>${project.build.directory}</outputDirectory>
              </fileset>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1.1</version>
        <executions>
          <execution>
            <id>compile-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target/cpp/Windows_x64</workingDirectory>
              <arguments>
                <argument>-Wall</argument>
                <argument>-m64</argument>
                <argument>-c</argument>
                <argument>-g3</argument>
                <argument>-std=c++0x</argument>
                <argument>-I../../myLib/headers</argument>
                <argument>../../../src/main/cpp/*.cpp</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>link-Windows_x64</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>g++</executable>
              <workingDirectory>target</workingDirectory>
              <arguments>
                <argument>-m64</argument>
                <argument>-s</argument>
                <argument>-oMyProg_Windows_x64.exe</argument>
                <argument>cpp/Windows_x64/*.o</argument>
                <argument>MyLib_Windows_x64.dll</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>none</phase>
          </execution>
        </executions>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Thankx


Solution

  • Eclipse does not use the Maven build. Instead it configures the JDT according to your POM. This is done through an M2E (Maven to Eclipse) connector. If you want the same to work for CDT, then you would need an M2E connector for that. I'm not aware of an existing one, so you would have to write an Eclipse plugin for that. See http://wiki.eclipse.org/M2E/Extension_Development for details on how to do that.

    Of course, you could also use "execute" instead of "ignore" in your lifecycleMapping in the pom. But that would probably make for a pretty slow build, because the whole build would be triggered on each change. And still this wouldn't "magically" make the errors from Maven appear in your files.