如何使用IntelliJ IDEA IDE创建使用Maven构建并包含log4j的Scala可执行jar文件



长期的Java开发人员试图学习Scala(不是使用Spark)。我想构建一个基本的 Scala 程序,它可以使用 Maven 构建并作为可执行 jar 运行。此外,我想继续将 Log4j 合并到其中,以便我有一个构建独立 jar 的基本框架。我在谷歌上搜索我需要做的所有部分时遇到了一些麻烦,但我终于想通了,所以我想发布如何做到这一点供其他人使用。

所以,事实证明我不得不使用 Log4j2(我从未使用过)而不是 Log4j(我用了很多)。因此,我在这里使用的基本环境是:

操作系统 = 视窗 10

IDE = IntelliJ IDEA 2018.1.6(社区版)

Java SDK = 1.8.0_111

语言 = Scala 2.12.6

构建工具 = Maven

步骤: 1. 转到文件 ->新项目 选择一个 Maven 项目,然后net.alchim31.maven:scala-archetype-simple archetype

开始一个新的 Maven 项目

在下一页上填写组 ID、项目 ID 和版本:

groupId、artifactId 和版本

然后在接下来的 2 页中使用默认值,然后单击"完成">

2.) 导入 Maven 依赖项: 当项目首次打开时,您应该会在右下角看到一个小弹出窗口,单击"启用自动导入":

自动导入

3.) 在项目的 main/scala 文件夹中创建一个资源文件夹 4.) 转到文件 -> 项目结构: 在此窗口中,您要将此 src/main/resources 路径添加为资源文件夹, 因此,在"项目结构"窗口中,单击"模块",然后单击"资源",然后单击"Rick"单击" 您的资源文件夹,然后选择"资源"。 然后,您应该在右侧看到紫色文件夹作为资源文件夹,单击"应用",然后单击"确定":

"项目结构"窗口

5.) 在你的 src/main/scala 文件夹中制作一个名为"classes"的新包

6.) 在 src/main/resources 文件夹中,创建一个名为 log4j2.properties 的新配置文件。这适用于 log4j 配置。

7.) 在此文件中,在其中输入以下信息以配置名为msggen的滚动日志文件.log该文件每 10MB 扮演一次角色:

name = PropertiesConfig
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = /home/ubuntu/logs/msggen/msggen.log
appender.rolling.filePattern = /home/ubuntu/logs/msggen/msggen.%d{dd-MMM}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = msgGenLog
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

8.) 在你的pom.xml中,你需要添加log4j2依赖项:

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-scala_2.12</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>

9.) 您还想在想要生成可执行 jar 时添加构建插件。 在构建和插件标签中,添加以下插件标签(将finalName标签更改为您想要的 jar 名称):

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>msggen</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.scala.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

10.)在你之前创建的src/main/scala/classes文件夹中,使用以下代码创建一个名为LogUtil.scala的Scala对象:

package classes
import org.apache.logging.log4j._
object LogUtil {
val msggenLogger:Logger = LogManager.getLogger("msgGenLog");
def msggenLoggerDEBUG(message: String): Unit = {
this.msggenLogger.debug(message)
}
}

11.) 更改您的App.scala对象以调用您的记录器,这是我的代码:

package com.scala
import classes.LogUtil
/**
* @author ${user.name}
*/
object App {
def main(args : Array[String]) {
LogUtil.msggenLoggerDEBUG("Hi there!");
}
}

12.) 右键单击App.scala对象并选择"运行'App'"以测试日志文件是否已生成和填充(设置 config 属性appender.rolling.fileName以设置要创建日志文件的位置):

测试日志消息

要构建 JAR 文件,请执行以下操作:

1.) 再次转到 File -> 项目结构,选择左侧的"工件"标签,然后单击"+"按钮,然后单击具有依赖项的模块中的 JAR ->:

项目的项目结构

2.) 在"从模块创建 JAR"窗口中,选择您的App.scala类作为主类,并确保将 src/main/resources 文件夹指定到您希望在其中制作清单文件的位置(从src/main/scala更改它):

从模块窗口创建 JAR

3.) 单击"确定"并记下"输出目录"字段,该字段告诉您制作完成后的罐子将在哪里。

4.) 单击"应用",然后单击"确定"。

5.)在主窗口中,转到"构建"->构建工件->然后在弹出的小窗口中,再次选择"构建">

6.)现在,在前面提到的输出目录路径中,您应该会看到jar文件。

7.)我在Windows上,所以打开命令行并转到jar所在的位置,然后通过键入来运行它

Java -jar msggen.jar

8.) 再次转到日志文件以验证是否已记录测试消息:

包含第二条消息的日志文件

所以,现在你应该有一个Scala应用程序的基本框架,其中包含Maven和Log4j2集成。

这是我的完整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.scala</groupId>
<artifactId>msggen</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>My wonderfull scala app</description>
<inceptionYear>2018</inceptionYear>
<licenses>
<license>
<name>My License</name>
<url>http://....</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.12.6</scala.version>
<scala.compat.version>2.12</scala.compat.version>
<spec2.version>4.2.0</spec2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-scala_2.12</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.compat.version}</artifactId>
<version>${spec2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
<version>${spec2.version}</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.3.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<!-- Tests will be run with scalatest-maven-plugin instead -->
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>TestSuiteReport.txt</filereports>
<!-- Comma separated list of JUnit test class names to execute -->
<jUnitClasses>samples.AppTest</jUnitClasses>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

最新更新