简体   繁体   中英

How to configure a manifest file of a jar file by using the maven assembly plugin?

how can I configure the content of a manifest file, which is include in a Jar file? Now I am using the maven assembly plugin to configure my jar file. I have defined some XML Tags, which includes pom.xml´s different values.

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>
                        jar-with-dependencies
                    </descriptorRef>
                </descriptorRefs>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifestEntries>                                   
                        <Source-JDK>${source.jdk}</Source-JDK>
                        <Target-JDK>${target.jdk}</Target-JDK>
                        <GroupId.ArtifactId>${project.groupId}.${project.artifactId}</GroupId.ArtifactId>
                        <Project.Version>${project.version}</Project.Version>                           
                        <Project.Build.SourceEncoding>UTF-8</Project.Build.SourceEncoding>  
                        <Maven.Build.Timestamp>${maven.build.timestamp}</Maven.Build.Timestamp>
                    </manifestEntries>
                    <manifest>                      
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>${project.groupId}.myMainClass</mainClass>
                    </manifest>
                </archive>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>assembly-jar-Id</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

In this solution, the manifest file of my jar file includes all tags, which are important for me. But if I use one of these specific tags, I get an error message likte this during executing my jar file:

 Error: An unexpected error occurred while trying to open file ./myJarFile.jar

I can solve this problem, if I delete all specific tags in the xml tag "manifestEntries".

This could be related to the use of invalid characters in the manifest attribute names. Valid characters are [0-9a-zA-Z_-] . This is discussed in the JavaDocs for the Attributes class:

http://docs.oracle.com/javase/7/docs/api/java/util/jar/Attributes.html

Also see the jar file specification, particularly the grammar specification for headerchar :

http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Name-Value_pairs_and_Sections

Some of your attribute names don't match the specified pattern, such as GroupId.ArtifactId . I recommend testing removal of each attribute one by one (or changing names to fit the spec) to confirm if particular attribute names are causing the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM