创建jar文件的问题

时间:2016-09-04 22:06:30

标签: java maven jar

我正在尝试创建Java Maven项目的jar文件。我尝试了很多但我似乎无法使它工作。

我得到的错误是“jar 的路径中没有主要的清单属性”。

我使用这个插件来创建jar,然后再构建它。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>Main</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我的主类简称为Main,它在MANIFEST.MF文件中显示为Main-Class:Main,我认为应该是正确的。

我感谢你的每一条建议,谢谢。

2 个答案:

答案 0 :(得分:0)

这是我的配置。可能很有用

的pom.xml

<pluginManagement>  
            <plugins>  
                <plugin>  
                    <groupId>org.eclipse.m2e</groupId>  
                    <artifactId>lifecycle-mapping</artifactId>  
                    <version>1.0.0</version>  
                    <configuration>  
                        <lifecycleMappingMetadata>  
                            <pluginExecutions>  
                                <pluginExecution>  
                                    <pluginExecutionFilter>  
                                        <groupId>org.apache.maven.plugins</groupId>  
                                        <artifactId>maven-dependency-plugin</artifactId>  
                                        <versionRange>[2.0,)</versionRange>  
                                        <goals>  
                                            <goal>run</goal>
                                            <goal>copy-dependencies</goal>
                                            <goal>single</goal>
                                        </goals>  
                                    </pluginExecutionFilter>  
                                    <action>  
                                        <ignore />  
                                    </action>  
                                </pluginExecution>  
                            </pluginExecutions>  
                        </lifecycleMappingMetadata>  
                    </configuration>  
                </plugin>  
            </plugins>  
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>  
                    <execution>  
                        <phase>process-resources</phase>  
                        <goals>  
                            <goal>run</goal>  
                        </goals>

                        <configuration>  
                            <tasks>
                                <copy file="${project.basedir}/script/startup.sh" 
                                    tofile="${project.build.directory}/bin/startup.sh" overwrite="true"/>
                                 <copy file="${project.build.directory}/classes/config.properties" 
                                    tofile="${project.build.directory}/conf/config.properties"/>
                                 <copy file="${project.build.directory}/classes/jdbc.properties" 
                                    tofile="${project.build.directory}/conf/jdbc.properties"/>
                            </tasks>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>../lib/</classpathPrefix>

                            <mainClass>com.bootstrap.Bootstrap</mainClass>
                        </manifest>
                    </archive>
                    <outputDirectory>${project.build.directory}/bin</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>

                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    </plugins>

答案 1 :(得分:-1)

关于您的配置的一些评论:

1)您的插件似乎有一个无效的XML。根据{{​​3}},它应该是这样的:

<project>
    [...]
    <build>
        [...]
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            [...]
        </plugins>
    </build>
</project>

2)mainClass代码应包含完整的合格主要类别,例如com.your.package.Main。除非您的Main类不在任何包装下,否则您可能希望更改它。