简体   繁体   中英

Using profiles in Netbeans and Maven multimodule project to build for Windows and Linux

In a NetBeans "workspace" for Java there are several projects (all dirs are on same level):

  • Main
  • MainExecutable (contains the main(String[] args) method for debugging)
  • JNILibrary (only available for Linux)
  • 10 further projects

And I am trying to make it debuggable on Windows by providing "stubs" (just some empty methods) source code for Windows through the profile in JNILibrary\\pom.xml :

<groupId>com.example</groupId>
<artifactId>jnilibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
    <groupId>com.example.test</groupId>
    <artifactId>Main</artifactId>
    <version>0.0.1-SNAPSHOT</version>        
    <relativePath>../Main/pom.xml</relativePath>
</parent>

<build>
    <sourceDirectory>${src.dir}</sourceDirectory>
</build>

<profiles>
    <profile>
        <id>windows</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <src.dir>src/windows/java</src.dir>
        </properties>
    </profile>
    <profile>
        <id>linux</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <src.dir>src/linux/java</src.dir>
        </properties>
    </profile>
</profiles>

After that I am able to select Set Configuration -> linux or Set Configuration -> Windows , when I right-click on the JNILibrary project in NetBeans.

Unfortunately, when I try to debug the MainExecutable project - it still (builds and) starts the Linux version - and fails to start on my Windows PC.

Could someone please point into right direction on how to solve my problem?

Here is an excerpt from Main\\pom.xml :

<groupId>com.example.test</groupId>
<artifactId>Main</artifactId>
<version>0.0.1-SNAPSHOT</version>   

<packaging>pom</packaging>
<name>Main</name>
<modules>
    <module>../MainExecutable</module>
    <module>../jnilibrary</module>
    <module>../10/further/modules</module>
</modules>

And here an excerpt from MainExecutable\\pom.xml :

<parent>
    <groupId>com.example.test</groupId>
    <artifactId>Main</artifactId>
    <relativePath>../Main</relativePath>
    <version>0.0.1-SNAPSHOT</version>       
</parent>
<artifactId>MainExecutable</artifactId>
<packaging>jar</packaging>

As you wrote, move the 2 profiles to Main/pom.xml and add to both of them new property jnilibrary.src.dir

<profiles>
    <profile>
        <id>windows</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <jnilibrary.src.dir>src/windows/java</src.dir>
        </properties>
    </profile>
    <profile>
        <id>linux</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <properties>
            <jnilibrary.src.dir>src/linux/java</src.dir>
        </properties>
    </profile>
</profiles>

and use that property in JNILibrary\\pom.xml

<build>
    <sourceDirectory>${jnilibrary.src.dir}</sourceDirectory>
</build>

You can use the OS Name to activate the profile:

<build>
    <sourceDirectory>${src.dir}</sourceDirectory>
</build>

<profiles>
    <profile>
        <id>windows</id>
        <activation>
            <os>
                <name>Windows</name>
            </os>
        </activation>
        <properties>
            <src.dir>src/windows/java</src.dir>
        </properties>
    </profile>
    <profile>
        <id>linux</id>
        <activation>
            <os>
                <name>Linux</name>
            </os>
        </activation>
        <properties>
            <src.dir>src/linux/java</src.dir>
        </properties>
    </profile>
</profiles>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM