maven打包替换依赖包的配置文件

解决Maven打包时依赖的jar包配置文件被覆盖问题,通过assembly.xml配置排除核心配置如zookeeper.properties等,确保项目自定义配置优先。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

今天遇到个头疼的问题,是zk的配置文件zookeeper.properties被替换了,选择maven,profiles没用。后来发现,打进jar包里的是依赖的jar包的配置文件,被覆盖了。因为提交spark任务的jar包通常是jar-with-dependencies。既需要这个jar又想用自己项目里的properties。

解决方案是:通过assembly.xml文件的方式设置打包需要的jar包内容

pom设置


    ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptors>
                        <descriptor>assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
    ...

assembly.xml文件

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <unpackOptions>
        <excludes>
          <exclude>core-site.xml</exclude>
          <exclude>hdfs-site.xml</exclude>
          <exclude>hbase-site.xml</exclude>
          <exclude>zookeeper.properties</exclude>
          <exclude>*.csv</exclude>
        </excludes>
      </unpackOptions>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>/</outputDirectory>
      <directory>target/classes</directory>
    </fileSet>
  </fileSets>
</assembly>

总结

之前用azkaban打zip包的时候也用到过这个assemnbly的用xml文件来定义打包的方式。现在发现可能通过unpackOptions来过滤依赖jar包一些文件。记录一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值