对于scala项目,Maven编译回复'无编译源'

时间:2013-10-30 15:58:31

标签: scala maven

我有以下(单个)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编译实现。但对于混合java / scala项目来说,这将是一个问题。

5 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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

看起来好像已将Scala编译绑定到process-resources阶段。关于无源的输出来自编译Java的常规maven编译器插件。

答案 2 :(得分:1)

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

SRC   主要     - java     - 斯卡拉     - 资源   测试     - java     - 斯卡拉     - 资源

etc ...基本上是一个理智的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>

答案 3 :(得分:0)

要向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视为源目录。

答案 4 :(得分: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>