Maven compile 为 Scala 项目回复“没有可编译的源代码”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19687821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven compile replies 'No sources to compile' for scala project
提问by javadba
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. But that would be an issue for mixed java/scala projects.
这甚至可以通过 mvn compile 工作。但对于混合的 java/scala 项目来说,这将是一个问题。
采纳答案by DB5
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/scalaas 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/javaand src/main/scalaare considered source directories by maven.
对于标准的 Maven 项目,这意味着现在src/main/java和src/main/scala都被 Maven 视为源目录。
回答by Jonny Coombes
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>
回答by johanandren
Have you followed the configuration steps in the maven scala plugin homepage? http://davidb.github.io/scala-maven-plugin/usage.html
你有没有按照maven scala插件主页中的配置步骤操作? 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. The output about no sources is from the regular maven compiler plugin that compiles Java.
看起来你已经将 Scala 编译绑定到进程资源阶段给我。关于无源的输出来自编译 Java 的常规 maven 编译器插件。
回答by Jonny Coombes
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 -- 资源测试 -- java -- scala -- 资源
etc...basically a sane Maven directory structure and then configure your build section like this:
等等...基本上是一个健全的 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>
回答by Yichuan Wang
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>

