我使用以下详细信息在 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。
参见:
- Apache Maven Shade Plugin – Executable JAR以及用于调整和配置的插件文档的其余部分
- 或旧的一 jar 工具 Deliver Your Java Application in One-JAR™ !
关于java - Scala和Maven项目的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50392597/