Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven only picks src/main/java or src/main/scala as source folder, never both

I'm using Eclipse 3.7 w/ m2e (installed 2 weeks ago), with Java 6 and Scala 2.10.

When ever I use m2e to update the project configuration, depending on how I have my .pom configured, it always either picks src/main/java && src/test/java or it picks src/main/scala && src/test/scala as my source folders. I would like it to have all four as source folders.

Here is my .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>me.my.name</groupId>
<artifactId>ai.chess</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>chessAI</name>
<description>Chess AI</description>

<repositories>
    <repository>
        <id>scala-tools.org</id>
        <name>Scala-tools Maven2 Repository</name>
        <url>http://scala-tools.org/repo-releases</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>scala-tools.org</id>
        <name>Scala-tools Maven2 Repository</name>
        <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
</pluginRepositories>

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
    <!-- <sourceDirectory>src/main/scala</sourceDirectory> 
    <testSourceDirectory>src/test/scala</testSourceDirectory> -->
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jvmArgs>
                    <jvmArg>-Xms64m</jvmArg>
                    <jvmArg>-Xmx1024m</jvmArg>
                </jvmArgs>
                <sources>
                    <source>src/main/scala</source>
                    <source>src/main/java</source>
                    <source>src/test/scala</source>
                    <source>src/test/java</source>
                </sources>
            </configuration>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.scala-tools
                                    </groupId>
                                    <artifactId>
                                        maven-scala-plugin
                                    </artifactId>
                                    <versionRange>
                                        [2.15.2,)
                                    </versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Eventually, I would like to create an UberJar with all the necessary dependencies so it can run on a server that doesn't support scala (but does Java). The framework for the Chess game is given in Java, so I would like to use that along side with Scala

I may just switch back to using Ant to build if Maven continues to be a pain.

P.S. With the given .pom it uses the java source folders

like image 908
Tombstone Avatar asked Mar 06 '13 00:03

Tombstone


2 Answers

The scala-maven-plugin (previously named the maven-scala-plugin) requires some extra configuration to do mixed Scala/Java projects, but if you follow their instructions (copied below), it should add all the necessary directories to your build path.

<plugins>
    <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>scala-compile-first</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>add-source</goal>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>scala-test-compile</id>
                <phase>process-test-resources</phase>
                <goals>
                    <goal>testCompile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <executions>
            <execution>
                <phase>compile</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
like image 114
Steve Avatar answered Nov 09 '22 20:11

Steve


You have two options: the first is to split your project in a parent project with two modules, each of them with its own pom. This is the more natural way to do that with maven but as I am not a scala user myself, I not completely sure is feasible in your setup.

 <parent> ai.chess (packaging: pom)
    <module> ai.chess.java (packaging: jar)
    <module> au.chess.scala (using <sourceDirectory>)

The second option is keep the layout as it is, and use build-helper-maven-plugin to add source directories to the build; like this:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/main/scala</source>
              </sources>
            </configuration>
          </execution>
          <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/test/scala</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
like image 40
guido Avatar answered Nov 09 '22 22:11

guido