Hiding manifest entries with maven
Asked Answered
P

4

14

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.

Prophetic answered 2/8, 2014 at 18:47 Comment(1)
I have the same question even though it is not limited to the Built-By in particular and I have not found any solution. I am putting a bounty on this question, because I have also found exactly what you have found in the comments to the only answer this question has.Diathermy
C
9

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

Cheadle answered 2/8, 2014 at 18:47 Comment(8)
How about use of maven shade plugin?Cheapjack
I don't see how to use the maven shade plugin to accomplish what the question is asking. If you know how to do that, please submit the details as an answer, I would be interested in seeing it.Cheadle
The maven-achiver plugin documentation seems pretty clear that you cannot remove properties. I don't agree, in the documentation page you linked to it says nothing about removing properties. It does describe what is default, but that does not necessarily mean you can't remove them. However that does not mean your answer is not right though and it really does seem to be so.Diathermy
Well, the documentation doesn't say anything about removing properties, only adding them. That seems pretty clear as indicating that you cannot remove properties using the maven-archiver plugin. That was what I meant.Cheadle
As I have encountered many incomplete documentations before I never take it for granted. In this case I have not seen anything on how to solve this issue and the only way would be to get the plugin changed. Since I have no doubt that you are right about your answer, the bounty is yours.Diathermy
@RichardNeish I've added a patch into the MSHARED-362 Jira ticket you mentioned in your answer. Hope it will be accepted.Timothee
I am working on all of them, soon you'll be able to drop all of them except Manifest-Version.Mimetic
Thanks @Mimetic and others - I've been following the discussion on MSHARED-362 and MSHARED-787, great work.Cheadle
B
6

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>
Ballflower answered 2/8, 2014 at 19:13 Comment(5)
The second solution works (the entry is still in the manifest but entry however). The first solution doesn't, the default entries are still in the manifest for me. According to the documentation, addDefaultImplementationEntries is just for the Implementation-* entries.Prophetic
Arrh! My fault! use this to completely remove generated properties: <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> #13218813Ballflower
As far as I can see, that affects only the pom.xml that is included in the jar by default, but not the MANIFEST.MF.Prophetic
Then this is double fault) Just create your own empty file and use it as: <archive> <manifestFile>src/custom-manifest.txt</manifestFile> </archive>Ballflower
That just does the same as specifying entries in <manifestEntries>, the custom manifest gets merged with the default manifest but the entries don't go away.Prophetic
S
3

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).

Shrew answered 9/1, 2019 at 21:26 Comment(0)
S
0

This removes all default entries from manifest

<addDefaultEntries>false</addDefaultEntries>

Example

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addDefaultEntries>false</addDefaultEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>
Subjectivism answered 6/5, 2022 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.