4

我有以下(单个)scala 类

[      14253 Oct 30  8:44]  ./pom.xml
[       9083 Oct 30  8:30]  ./scaladem.iml
[        102 Oct 29 19:21]  ./src
[        102 Oct 29 19:21]  ./src/main
[        102 Oct 29 19:21]  ./src/main/scala
[        102 Oct 29 19:21]  ./src/main/scala/com
[        102 Oct 29 19:21]  ./src/main/scala/com/blazedb
[        102 Oct 30  8:30]  ./src/main/scala/com/blazedb/scalademo
[       4646 Oct 30  8:30]  ./src/main/scala/com/blazedb/scalademo/SDemo.scala

这是pom的适用部分

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.1.6</version>
        <configuration>
          <recompileMode>incremental</recompileMode>
          <javacArgs>
            <javacArg>-Xlint:unchecked</javacArg>
            <javacArg>-Xlint:deprecation</javacArg>
          </javacArgs>
        </configuration>
    <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>

当我们跑

mvn compile

我们得到(注意'没有来源'..)

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SDemo 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ scalademo ---
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

更新:运行以下命令时

$mvn scala:compile -DdisplayCmd=true

编译成功。

根据@badtrumpet 的建议,我添加了如下所示的显式

<sourceDirectory>src/main/scala</sourceDirectory>

这甚至可以通过 mvn compile 进行。但这对于混合 java/scala 项目来说将是一个问题。

4

5 回答 5

5

这是一个示例(简单)pom.xml,我将其用作 Scala 编译和使用 Maven 构建的样板:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>badtrumpet</groupId>
<artifactId>blog</artifactId>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2013</inceptionYear>
<packaging>jar</packaging>
<properties>
    <scala.version>2.10.2</scala.version>
    <commons.codec.version>1.8</commons.codec.version>
    <grizzled.version>1.0.1</grizzled.version>
    <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version>
</properties>

<repositories>
    <repository>
        <id>Sonatype repository</id>
        <name>Sonatype's Maven repository</name>
        <url>http://oss.sonatype.org/content/groups/public</url>
    </repository>
    <repository>
        <id>scala-tools.org</id>
        <name>Scala-Tools Maven2 Repository</name>
        <url>http://scala-tools.org/repo-releases</url>
    </repository>
    <repository>
        <id>milestone.repo.springsource.org</id>
        <name>repo.springsource.org-milestone</name>
        <url>https://repo.springsource.org/libs-milestone</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>${scala.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons.codec.version}</version>
    </dependency>
    <dependency>
        <groupId>org.clapper</groupId>
        <artifactId>grizzled-slf4j_2.10</artifactId>
        <version>${grizzled.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j-log4j12.version}</version>
    </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>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
            </configuration>
        </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
            </configuration>
        </plugin>
    </plugins>
</reporting>

于 2013-10-30T16:32:12.087 回答
1

您是否按照 maven scala 插件主页中的配置步骤进行操作? http://davidb.github.io/scala-maven-plugin/usage.html

看起来好像您已将 Scala 编译绑定到我的流程资源阶段。关于无源的输出来自编译 Java 的常规 maven 编译器插件。

于 2013-10-30T16:09:53.870 回答
1

要将额外的源文件夹添加到您的 maven 项目,您可以使用build-helper-maven-plugin

因此,要添加src/main/scala为源文件夹,您将在 pom 中配置以下内容:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${basedir}/src/main/scala</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

对于标准的 maven 项目,这意味着现在两者src/main/javasrc/main/scala都被 maven 视为源目录。

于 2013-10-31T07:59:59.940 回答
1

要混合 Java 和 scala,只需确保您的源代码树类似于:

src main -- java -- scala -- 资源测试 -- java -- scala -- 资源

等等...基本上是一个健全的 Maven 目录结构,然后像这样配置您的构建部分:

<build>
    <sourceDirectory>src/main/</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
于 2013-11-01T01:34:55.617 回答
0

由于某种原因,官方文档对我不起作用,我必须添加以下内容 <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions>

所以我在标签下的标签看起来像这样: <build> <!-- To define the plugin version in your parent POM --> <pluginManagement> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.0</version> </plugin> </plugins> </pluginManagement> <!-- To use the plugin goals in your POM or parent POM --> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

于 2014-09-30T00:34:58.367 回答