7

我正在使用带有 Java 6 和 Scala 2.10 的 Eclipse 3.7 w/m2e(两周前安装)。

每当我使用 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 来构建。

PS 对于给定的 .pom,它使用 java 源文件夹

4

3 回答 3

7

scala-maven-plugin(以前称为 maven-scala-plugin)需要一些额外的配置来执行混合 Scala/Java 项目,但是如果您按照他们的说明(复制如下),它应该将所有必要的目录添加到您的构建路径.

<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>
于 2013-03-06T10:46:46.783 回答
4

您有两个选择:第一个是将您的项目拆分为具有两个模块的父项目,每个模块都有自己的 pom.xml 文件。这是使用 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>
于 2013-03-06T00:41:03.700 回答
0
  1. 更新到 scala-maven-plugin (scala-tools.org 不再存在,因为 2 年)
  2. 使用目标添加源(仅对 scala 执行与 build-helper-maven-plugin 相同的操作)

您还可以将 .java 和 .scala 文件存储在同一个 src/main/xxx 和 src/test/xxx 下,java 编译器忽略 *.scala,scala 编译器同时使用这两者。

于 2013-03-11T11:00:54.330 回答