在Java Maven依赖项中包含Scala项目

时间:2016-03-29 12:37:27

标签: java eclipse scala maven

我是scala的新手,我试图将scala项目包含到我的java项目中。 我有单独的scala和java项目,我在java中使用scala。

当我将scalaTest添加到我的java项目构建路径时,它工作正常。有没有什么办法可以编辑我的pom.xml,在maven构建时自动引用scalaTest项目?。

我在我的pom.xml中添加了scala依赖项

或者有什么方法可以将java和scala文件都包含在我的java项目中并运行吗?

enter image description here

2 个答案:

答案 0 :(得分:2)

如果您使用以下pom.xml,您应该能够通过mvn从同一项目中编译Java和Scala源。

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>javaScala</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>javaScala</name>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
             <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourceDir>src/main/scala</sourceDir>
                    <testSourceDir>src/test/scala</testSourceDir>
                    <jvmArgs>
                        <jvmArg>-Xms64m</jvmArg>
                        <jvmArg>-Xmx1024m</jvmArg>
                    </jvmArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

答案 1 :(得分:0)

回答我自己的问题

  1. 在java项目的类路径(buildpath)中添加Scala项目。
  2. 将Scala项目导出为jar并导入到您的java项目中。
  3. 要在单个java项目中包含java和scala,请遵循以下步骤。

    将java文件包含在 - src / main / java中 将scala文件包含在--src / main / scala中 使用此pom.xml

    https://github.com/rthoma24/Java-Scala.git下载项目     

    maven命令

      

    mvn编译
      mvn exec:java -Dexec.mainClass = main.java.Test

    <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>ted-gao</groupId>
        <artifactId>scala-java-mix</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Scala-Java mixture</name>
        <description>Showcase mixing Scala and Java</description>
        <packaging>jar</packaging>
    
        <build>
            <plugins>
                <!-- ensure that we use JDK 1.6 -->
                <plugin>
                    <inherited>true</inherited>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.scala-tools</groupId>
                    <artifactId>maven-scala-plugin</artifactId>
                    <version>2.15.2</version>
                    <executions>
                        <!-- Run scala compiler in the process-resources phase, so that dependencies on
                             scala classes can be resolved later in the (Java) compile phase -->
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>                        
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            </execution>
    
                            <!-- Run scala compiler in the process-test-resources phase, so that dependencies on
                                 scala classes can be resolved later in the (Java) test-compile phase -->                    
                            <execution>
                            <id>scala-test-compile</id>
                            <phase>process-test-resources</phase>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <executions>
                        <!-- Add src/main/scala to source path of Eclipse -->
                        <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>
    
                        <!-- Add src/test/scala to test source path of Eclipse -->
                        <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>
    
                <!-- to generate Eclipse artifacts for projects mixing Scala and Java -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.8</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                        <projectnatures>
                            <projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
                            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
                        </projectnatures>
                        <buildcommands>
                            <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
                        </buildcommands>
                        <classpathContainers>
                            <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer>
                            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                        </classpathContainers>
                        <excludes>
                            <!-- in Eclipse, use scala-library, scala-compiler from the SCALA_CONTAINER rather than POM <dependency> -->
                            <exclude>org.scala-lang:scala-library</exclude>
                            <exclude>org.scala-lang:scala-compiler</exclude>
                        </excludes>
                        <sourceIncludes>
                            <sourceInclude>**/*.scala</sourceInclude>
                            <sourceInclude>**/*.java</sourceInclude>
                        </sourceIncludes>
                    </configuration>
                </plugin>
    
                <!-- When run tests in the test phase, include .java and .scala source files -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.8.1</version>
                    <configuration>
                        <includes>
                            <include>**/*.java</include>
                            <include>**/*.scala</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <dependencies>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>2.9.0</version>
            </dependency>
        </dependencies>
    </project>
    

    如果mvn install / compile出现任何错误,请删除m2目录并重试。