java - Scala和Maven项目的执行

标签 java scala maven

我使用以下详细信息在 eclipse 的 scala ide 中创建了一个简单的 Scala Maven 项目 -

Artifact 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内添加了 list 条目标签。

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

我只有一个App.scala项目中可用的文件和一个测试文件。构建成功。但是当尝试使用 java -jar <jar_name> 执行 jar 时低于错误 -

no main manifest attribute, in scalapoc-0.0.1.jar

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

Error: Could not find or load main class com.test.scalapoc.App.scala

请建议执行该 jar 需要什么。

最佳答案

尝试

        <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 仅适用于源代码,源代码不可执行
  • 归档标签是插件配置的一部分

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

2018-06-05 更新:

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

参见:

关于java - Scala和Maven项目的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50392597/

相关文章:

java - 如何仅设置具有流式布局的面板的首选宽度?

maven - UmlGraph 没有自动集成到 JavaDoc 中

java - 如何使用 Maven 将类路径添加到 list 文件

java - 从另一个 JFrame 执行 JFrame

java - Hibernate 要求 javax.validation.constraints.Size 进行枚举

json - 如何跟踪通过 elastic4s 客户端发送到 Elasticsearch 的 json 请求?

hibernate - 无法初始化上下文,因为已经存在根应用程序上下文

java - 创建并填充可变大小的二进制真值表

java - 传递泛型并且必须转换为正确的对象java。不应该转换

scala - scala 中的隐式提升