Hiding manifest entries with maven

2019-04-04 16:39发布

When building a jar file with maven, it will create a manifest file in META-INF/MANIFEST.MF. Its contents currently are:

Manifest-Version: 1.0                                                                    
Archiver-Version: Plexus Archiver
Built-By: <my username>
Created-By: Apache Maven 3.1.0
Build-Jdk: 1.8.0_5

How can I hide manifest entries? In particular I would like to hide the "Built-By:" entry because I don't see any reason why a jar should include my username.

3条回答
成全新的幸福
2楼-- · 2019-04-04 17:17

Set user.name variable to empty string.

mvn -Duser.name="" package

Or if you are using Eclipse add -Duser.name="" to VM arguments of Maven run configuration (Run -> Run As -> Maven build... -> JRE tab -> enter -Duser.name="" in VM arguments field).

查看更多
疯言疯语
3楼-- · 2019-04-04 17:18

The maven-achiver plugin documentation seems pretty clear that you cannot remove properties, only set them to blank as described in Alex Chernyshev's answer. In order to get more control over the MANIFEST.MF you should not use the maven-archiver plugin.

One alternative would be to use the maven antrun plugin and the Ant Jar Task to build the Jar.

In pom.xml:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>package</phase>
            <configuration>
              <target>
                <jar destfile="test.jar" basedir=".">
                  <include name="build"/>
                  <manifest>
                    <attribute name="Manifest-Version:" value="1.0"/>
                  </manifest>
                </jar>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Another alternative is calling the Jar tool directly using the Maven exec plugin.

I don't like recommending antrun, as I think it's a dirty hack, but it seems that maven-archiver does not meet your requirements. It might be worth raising a feature request for maven-archiver.

EDIT: 2014-10-06 Raised Jira MSHARED-362

EDIT: 2018-06-18: Updated link to Jira and to Maven Exec plugin

EDIT: 2019-01-14: Fix targeted for maven-archiver-3.4.0

查看更多
三岁会撩人
4楼-- · 2019-04-04 17:29

Add

<addDefaultImplementationEntries>false</addDefaultImplementationEntries>

to configration section of maven-jar-plugin in your pom.xml to completely remove default properties:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
        </manifest>
      </archive>
    </configuration>
  </plugin>

to customize it (remove just username):

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Built-By></Built-By>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
查看更多
登录 后发表回答