执行Scala和Maven项目

时间:2018-05-17 13:23:19

标签: java scala maven

我使用以下细节在eclipse的scala ide中创建了一个简单的Scala maven项目 -

工件ID - scala-archetype-simple

群组ID - net.alchim31.maven

创建项目后,我修改了pom.xml文件。

<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>com.test</groupId>
    <artifactId>scalapoc</artifactId>
    <version>0.0.1</version>
    <name>${project.artifactId}</name>
    <description>Test scala app</description>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.11.5</scala.version>
        <scala.compat.version>2.11</scala.compat.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2-core_${scala.compat.version}</artifactId>
            <version>2.4.16</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2-junit_${scala.compat.version}</artifactId>
            <version>2.4.16</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_${scala.compat.version}</artifactId>
            <version>2.2.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <!-- see http://davidb.github.com/scala-maven-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>
                        <configuration>
                            <args>
                                <!-- <arg>-make:transitive</arg> -->
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                            <archive>
                                <manifest>
                                    <mainClass>App.scala</mainClass>
                                </manifest>
                            </archive>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>
                    <!-- If you have classpath issue like NoDefClassError,... -->
                    <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我还在plugin标记内添加了清单条目。

<archive>
    <manifest>
        <mainClass>App.scala</mainClass>
    </manifest>
</archive>

项目中只有一个App.scala文件和一个测试文件。构建成功。但是当尝试使用java -jar <jar_name>执行jar时,得到以下错误 -

  

scalapoc-0.0.1.jar

中没有主要的清单属性

当尝试执行命令java -cp scalapoc-0.0.1.jar com.test.scalapoc.App.scala低于错误 -

  

错误:无法找到或加载主类com.test.scalapoc.App.scala

请建议执行jar的操作。

1 个答案:

答案 0 :(得分:1)

尝试

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.test.scalapoc.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
  • .scala仅供参考,来源不可执行
  • archive标签是插件配置的一部分

请参阅https://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make

更新2018-06-05:

要创建独立jar(不需要在命令行上使用scala-library和其他依赖项定义类路径),您应该创建一个包含其他类/ jar的jar。

请参阅: