Maven只选择src / main / java或src / main / scala作为源文件夹,而不是两者

时间:2013-03-06 00:15:56

标签: java eclipse scala maven

我正在使用Eclipse 3.7 w / m2e(2周前安装),Java 6和Scala 2.10。

当我使用m2e更新项目配置时,根据我配置的.pom的方式,它总是选择src/main/java&& src/test/java或者选择src/main/scala&& src/test/scala作为我的源文件夹。我希望将所有四个作为源文件夹。

这是我的.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>

最终,我想创建一个具有所有必要依赖关系的UberJar,以便它可以在不支持scala(但是支持Java)的服务器上运行。国际象棋游戏的框架是用Java提供的,所以我想将它与Scala一起使用

如果Maven仍然很痛苦,我可能会切换回使用Ant来构建。

P.S。使用给定的.pom,它使用java源文件夹

3 个答案:

答案 0 :(得分:6)

scala-maven-plugin(previously named the maven-scala-plugin)需要some extra configuration to do mixed Scala/Java projects,但如果您按照他们的说明(下面复制),它应该将所有必要的目录添加到您的构建路径。

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

答案 1 :(得分:4)

您有两个选择:第一个是在具有两个模块的父项目中拆分项目,每个模块都有自己的pom。这是使用maven做到这一点的更自然的方法,但由于我自己不是scala用户,因此我不能完全确定您的设置是否可行。

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

第二个选项是保持布局不变,并使用build-helper-maven-plugin将源目录添加到构建中;像这样:

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

答案 2 :(得分:0)

  1. 更新到scala-maven-plugin(scala-tools.org已不再存在,自2年以来)
  2. 使用目标add-source(仅对scala执行build-helper-maven-plugin)
  3. 您还可以将.java和.scala文件存储在相同的src / main / xxx和src / test / xxx下,java编译器忽略* .scala,scala编译器同时使用它们。