idea maven打包java、scala混合项目 & maven打包指定主类

1.maven打包java、scala混合项目

在使用 maven-shade-plugin 插件打包java、scala混合项目的过程中,发现执行mvn package命令打包的jar包中没有scala类,

经发现maven的默认compile不会编译scala类。

1.1

经搜索可以添加下面两个插件:

<plugin>
	<!-- 这是个编译java代码的 -->
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.2</version>
	<configuration>
		<source>1.8</source>
		<target>1.8</target>
		<encoding>UTF-8</encoding>
	</configuration>
	<executions>
		<execution>
			<phase>compile</phase>
			<goals>
				<goal>compile</goal>
			</goals>
		</execution>
	</executions>
</plugin>
<plugin>
	<!-- 这是个编译scala代码的 -->
	<groupId>net.alchim31.maven</groupId>
	<artifactId>scala-maven-plugin</artifactId>
	<version>3.2.1</version>
	<executions>
		<execution>
			<id>scala-compile-first</id>
			<phase>process-resources</phase>
			<goals>
				<goal>add-source</goal>
				<goal>compile</goal>
			</goals>
		</execution>
	</executions>
</plugin>

1.2

还可以使用idea的手动编译项目:

Build => Build Project,然后执行maven package命令。

 

2.maven打包指定主类

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>2.4.3</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
				<filters>
					<filter>
						<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>test.Test</mainClass>
					</transformer>
				</transformers>
			</configuration>
		</execution>
	</executions>
</plugin>

transformers配置中指定主类。

之后执行mvn package命令可以生成两个jar包:

包含依赖的jar包类主类已指定,使用压缩软件打开jar包查看META-INF/MANIFEST.MF文件

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值