应用系统定制开发maven scala java 混合项目编译、打包(jar包)、运行

文章目录

说明

应用系统定制开发一开始用的是在idea应用系统定制开发中打包的方式,应用系统定制开发但是在更新项目结构或应用系统定制开发是更新依赖后,应用系统定制开发需要手动的去修改原来的配置,应用系统定制开发不是很方便。于是查找资料看下用命令如何打包

java相关的插件

java代码编译与打包,我参考了这篇文章的内容,介绍的很详细:
需要引入的插件为maven-compiler-plugin(java编译插件)、maven-shade-plugin(java打包插件,用于将个人代码和第三方依赖打包到一个jar包中)

maven-shade-plugin 插件提供了两个能力:
把整个项目(包含它的依赖)都打包到一个 “uber-jar” 中
shade - 即重命名某些依赖的包
由此引出了两个问题:
什么是 uber-jar ?
uber-jar 也叫做 fat-jar 或者 jar-with-dependencies,意思就是包含依赖的 jar。
什么是 shade ?
shade 意为遮挡,在此处可以理解为对依赖的 jar 包的重定向(主要通过重命名的方式)。

如果要将个人代码和第三方依赖分开打包需要使用maven-jar-plugin、maven-dependency-plugin,可以参照这篇文章

相关的插件

scala代码编译与打包,我参考了这篇文章的内容,
需要引入的插件为maven-scala-plugin(scala编译插件)

maven命令打包

创建项目



在main路径下新增Diractory 并命名为scala,然后修改属性为source root

分别指定java和scala的source root路径

最初我的scala和java代码都放在java这个source root下,然后使用maven编译、打包时也顺利执行,但是在本地cmd中通过java -jar 或java -classpath运行jar包时总是提示找不到或无法加载主类
然后我将scala和java代码拆分后再打包,这个问题就没有出现了,用java -jar 或java -classpath运行jar包都可以正常得到输出。

添加package,分别创建java、scala 方法

scala 中main方法需定义在object 中,否则java -jar中运行时会提示

编辑pom文件

添加依赖、添加插件
完整pom文件如下所示
和编译、打包相关的配置都在<build> </build>标签内

<?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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.my</groupId>    <artifactId>test-package</artifactId>    <version>1.0-SNAPSHOT</version>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!-- 在这里定义你的入口类 -->        <mainClass>com.chinalife.sfrs.scala.gscRenewalPolicyDataParsing</mainClass>        <maven.compiler.source>8</maven.compiler.source>        <maven.compiler.target>8</maven.compiler.target>        <scala.version>2.11.0</scala.version>        <spark.version>2.4.0-cdh6.3.0</spark.version>        <spark_scala.version>2.11</spark_scala.version>        <hadoop.version>3.0.0-cdh6.3.0</hadoop.version>        <hive.version>2.1.1-cdh6.3.0</hive.version>        <log4j2.version>2.11.2</log4j2.version>        <hutool.version>5.6.7</hutool.version>    </properties>    <repositories>        <repository>            <id>alimaven</id>            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>        </repository>        <repository>            <id>alimaven2</id>            <url>https://maven.aliyun.com/repository/public</url>        </repository>        <repository>            <id>cloudera</id>            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>        </repository>    </repositories>    <dependencies>        <dependency>            <groupId>org.apache.spark</groupId>            <artifactId>spark-core_${spark_scala.version}</artifactId>            <version>${spark.version}</version>            <exclusions>                <exclusion>                    <artifactId>slf4j-api</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>                <exclusion>                    <artifactId>jcl-over-slf4j</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>            </exclusions>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->        <dependency>            <groupId>org.apache.spark</groupId>            <artifactId>spark-sql_${spark_scala.version}</artifactId>            <version>${spark.version}</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.scala-lang/scala-actors -->        <dependency>            <groupId>org.scala-lang</groupId>            <artifactId>scala-actors</artifactId>            <version>2.11.12</version>        </dependency>        <dependency>            <groupId>org.scala-lang</groupId>            <artifactId>scala-library</artifactId>            <version>${scala.version}</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-client</artifactId>            <version>${hadoop.version}</version>            <exclusions>                <exclusion>                    <artifactId>slf4j-api</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>            </exclusions>        </dependency>        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->        <dependency>            <groupId>commons-dbcp</groupId>            <artifactId>commons-dbcp</artifactId>            <version>1.4</version>        </dependency>        <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->        <dependency>            <groupId>commons-pool</groupId>            <artifactId>commons-pool</artifactId>            <version>1.6</version>        </dependency>        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->        <dependency>            <groupId>cn.hutool</groupId>            <artifactId>hutool-all</artifactId>            <version>${hutool.version}</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.scala-tools/maven-scala-plugin -->        <dependency>            <groupId>org.scala-tools</groupId>            <artifactId>maven-scala-plugin</artifactId>            <version>2.15.2</version>            <exclusions>                <exclusion>                    <artifactId>jcl-over-slf4j</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>                <exclusion>                    <artifactId>slf4j-api</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>            </exclusions>        </dependency>        <!--log4j2 核心包-->        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-api</artifactId>            <version>${log4j2.version}</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-core</artifactId>            <version>${log4j2.version}</version>            <scope>runtime</scope>        </dependency>        <!--如果现有组件使用Log4j 1.x并且您希望将此日志记录路由到Log4j 2,则删除所有log4j 1.x依赖项并添加以下内容-->        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-1.2-api</artifactId>            <version>${log4j2.version}</version>        </dependency>        <!--如果现有组件使用Apache Commons Logging 1.x并且您希望将此日志记录路由到Log4j 2,则添加以下内容但不删除任何Commons Logging 1.x依赖项。-->        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-jcl</artifactId>            <version>${log4j2.version}</version>        </dependency>        <!--如果现有组件使用SLF4J并且您希望将此日志记录路由到Log4j 2,则添加以下内容但不删除任何SLF4J依赖项。-->        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-slf4j-impl</artifactId>            <version>${log4j2.version}</version>        </dependency>        <!--为了正确支持和处理Web应用程序的ClassLoader环境和容器生命周期,需要一个额外的模块。该模块仅在运行时需要。此外,如果您在OSGi环境中使用servlet,请确保您的首选版本的servlet API已经可用(例如,如果您要使用3.0,但是您还加载了2.5,请确保它们都已加载)。-->        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-web</artifactId>            <version>${log4j2.version}</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.13</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-core-asl</artifactId>            <version>1.9.13</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.47</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.scala-tools</groupId>                <artifactId>maven-scala-plugin</artifactId>                <version>2.15.2</version>                <executions>                    <execution>                        <id>scala-compile-first</id>                        <goals>                            <goal>compile</goal>                        </goals>                        <configuration>                            <includes>                                <include>**/*.scala</include>                            </includes>                        </configuration>                    </execution>                    <execution>                        <id>scala-test-compile</id>                        <goals>                            <goal>testCompile</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.0</version>                <configuration>                    <source>8</source>                    <target>8</target>                    <encoding>UTF-8</encoding>                </configuration>                <executions>                    <execution>                        <phase>compile</phase>                        <goals>                            <goal>compile</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <executions>                    <!-- Run shade goal on package phase -->                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <filters>                                <filter>                                    <!-- Do not copy the signatures in the META-INF folder.                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->                                    <artifact>*:*</artifact>                                    <excludes>                                        <exclude>META-INF/*.SF</exclude>                                        <exclude>META-INF/*.DSA</exclude>                                        <exclude>META-INF/*.RSA</exclude>                                    </excludes>                                </filter>                            </filters>                            <transformers>                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <mainClass>${groupId}.${mainClass}</mainClass>                                </transformer>                            </transformers>                            <createDependencyReducedPom>false</createDependencyReducedPom>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273

执行命令

mvn clean package -DskipTests

命令中的clean、package分别对应工程生命周期中的clean、package阶段
-DskipTests指跳过test阶段

运行jar

idea打包






然后在项目的根路径下的out文件夹中的Artifact中可以看到编译打包好的jar包和其它第三方依赖

参考资料

这片对maven-shade-plugin的讲解很全面

这片介绍java编译、打包是最详细的一篇

maven-shade-plugin

资料:

resource transformers

如果类或资源没有重复出现,则直接将类或资源合并到一个jar包中输出,如果类或资源重复出现,择需要选择处理重复类或资源的方法,可选的方法有以下几种:
package:org.apache.maven.plugins.shade.resource

ApacheLicenseResourceTransformerPrevents license duplication
ApacheNoticeResourceTransformerPrepares merged NOTICE
AppendingTransformerAdds content to a resource
ComponentsXmlResourceTransformerAggregates Plexus components.xml
DontIncludeResourceTransformerPrevents inclusion of matching resources
GroovyResourceTransformerMerges Apache Groovy extends modules
IncludeResourceTransformerAdds files from the project
ManifestResourceTransformerSets entries in the MANIFEST
PluginXmlResourceTransformerAggregates Mavens plugin.xml
ResourceBundleAppendingTransformerMerges ResourceBundles
ServicesResourceTransformerRelocated class names in META-INF/services resources
XmlAppendingTransformerAdds XML content to an XML resource
网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发