简体   繁体   English

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

[英]Maven compile replies 'No sources to compile' for scala project

I have the following (single) scala class 我有以下(单个)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

Here is the applicable section of the pom 这是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>

When we run 我们跑的时候

mvn compile

We get (notice the 'no sources' ..) 我们得到(注意'没有来源'..)

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

Update: when running the following command 更新:运行以下命令时

$mvn scala:compile -DdisplayCmd=true

The compilation succeeds. 编译成功。

From suggestion of @badtrumpet I have added the explicit as shown below 根据@badtrumpet的建议,我添加了如下所示的显式

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

And this works even by mvn compile. 这甚至可以通过mvn编译来实现。 But that would be an issue for mixed java/scala projects. 但对于混合java / scala项目来说,这将是一个问题。

Here's an example (simple) pom.xml which I use as a bit of a boilerplate for Scala compilation and build using Maven: 这是一个示例(简单)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>

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

Looks as though you have bound the Scala compilation to the process-resources phase to me. 看起来好像已将Scala编译绑定到流程资源阶段。 The output about no sources is from the regular maven compiler plugin that compiles Java. 关于无源的输出来自编译Java的常规maven编译器插件。

To add extra source folders to your maven project you can use the build-helper-maven-plugin . 要向maven项目添加额外的源文件夹,可以使用build-helper-maven-plugin

So to add src/main/scala as a source folder you would configure the following in your pom: 因此,要将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>

For a standard maven project this would mean that now both src/main/java and src/main/scala are considered source directories by maven. 对于标准的maven项目,这意味着现在src/main/javasrc/main/scala都被maven视为源目录。

To mix Java and scala, just ensure your source tree is something like: 要混合Java和scala,只需确保您的源代码树是这样的:

src main -- java -- scala -- resources test -- java -- scala -- resources src main - java - scala - resources test - java - scala - resources

etc...basically a sane Maven directory structure and then configure your build section like this: 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>

For some reason the official document doesn't work for me, I have to add the following <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> 由于某种原因,官方文档对我不起作用,我必须添加以下<executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions>

so my tag under tag looks like this: <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> 所以标签下的标签如下所示: <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM