1

我在 Eclipse 中有一个使用 m2e 的 Maven 项目。我起诉春天。当 maven 构建 jar 文件时,我将所有依赖项 jar 复制到文件夹 dependency-jars。这里我是怎么做的。

<build>
    <!-- to avoid maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e error -->
    <pluginManagement>
        <plugins>   
            <!-- Ignore/Execute plugin execution -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                         <pluginExecutions>

                            <!-- copy-dependency plugin -->
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>

                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>

        <!-- Maven compiler plugin
             If you run the code maven package now, Maven will package this Java project into a jar file 
             named “test-1.0.0.jar“, in target folder. 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <!-- To make jar file like a exe file, you need to define a manifest file and declare the application 
             entry point inside via maven-jar-plugin in pom.xml. 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin.version}</version>

            <!-- The configuration of the plugin -->
            <configuration>

                <!-- Configuration of the archiver -->
                <archive>

                    <!-- Manifest specific configuration -->
                    <manifest>

                        <!-- Classpath is added to the manifest of the created jar file. -->
                        <addClasspath>true</addClasspath>

                        <!--
                           Configures the classpath prefix. This configuration option is
                           used to specify that all needed libraries are found under dependency-jars/
                           directory.

                           Use “classpathPrefix” to specify folder name in which all properties will be placed.
                       -->
                        <classpathPrefix>dependency-jars/</classpathPrefix>

                        <!-- Specifies the main class of the application -->
                        <mainClass>pk.training.basitMahmood.BatchImport</mainClass>
                    </manifest>


                </archive>
            </configuration>
        </plugin>

        <!--  uses maven-dependency-plugin to copy all dependencies to "target/dependency-jars/" folder, and 
              defines the dependency classpath with maven-jar-plugin 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeGroupIds>
                            log4j, org.slf4j, org.springframework, commons-net, commons-collections, 
                            org.apache.commons, javax.mail, org.apache.velocity, commons-logging
                        </includeGroupIds>
                        <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>


    </plugins>
</build>

直到一切正常。现在我有 2 个属性文件,我不想包含在 JAR 中,因为我不希望每次更改属性文件后都必须制作 jar。我希望属性文件不在 jar 中,并且它们的条目被添加到 Manifest 中,并且每次我执行 jar 时,它只是从 jar 外部的文件中读取属性。为此,我尝试了一些类似下面的东西。但我坚持如何在 中定义路径<manifest>,我已经为dependency-jars. 这是我所做的,但是我的程序没有运行。

<build>

    <!-- To exclude any file from a jar / target directory you can use the <exludes> tag in your pom.xml.
         all files with extention .properties will not be included: 
    -->
    <resources>
        <resource>
            <directory>${basedir}/src/main/java/pk/training/basitMahmood/util</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>*.properties</exclude>
            </excludes>
        </resource>

        <resource>
            <directory>src/main/resources/email</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>*.properties</exclude>
            </excludes>
        </resource> 
    </resources>

    <!-- to avoid maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e error -->
    <pluginManagement>
        <plugins>

            <!-- Ignore/Execute plugin execution -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                         <pluginExecutions>

                            <!-- copy-dependency plugin -->
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>

                            <!-- maven-antrun-plugin -->
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-antrun-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>run</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>

        <!-- Maven compiler plugin
             If you run the code maven package now, Maven will package this Java project into a jar file 
             named “test-1.0.0.jar“, in target folder. 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <!-- To make jar file like a exe file, you need to define a manifest file and declare the application 
             entry point inside via maven-jar-plugin in pom.xml. 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin.version}</version>

            <!-- The configuration of the plugin -->
            <configuration>

                <!-- Configuration of the archiver -->
                <archive>

                    <!-- Manifest specific configuration -->
                    <manifest>

                        <!-- Classpath is added to the manifest of the created jar file. -->
                        <addClasspath>true</addClasspath>

                        <!--
                           Configures the classpath prefix. This configuration option is
                           used to specify that all needed libraries are found under dependency-jars/
                           directory.

                           Use “classpathPrefix” to specify folder name in which all properties will be placed.
                       -->
                        <classpathPrefix>dependency-jars/</classpathPrefix>

                        <!-- Specifies the main class of the application -->
                        <mainClass>pk.training.basitMahmood.BatchImport</mainClass>
                    </manifest>

                    <!-- Use “Class-Path” to specify the folder. “.” Indicate current folder, while 
                         “propertiesFiles” specifies “propertiesFiles” folder in same directory as JAR. 
                    -->
                    <manifestEntries>
                        <Class-Path>.propertiesFiles</Class-Path>
                     </manifestEntries>

                </archive>
            </configuration>
        </plugin>

        <!--  uses maven-dependency-plugin to copy all dependencies to "target/dependency-jars/" folder, and 
              defines the dependency classpath with maven-jar-plugin 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeGroupIds>
                            log4j, org.slf4j, org.springframework, commons-net, commons-collections, 
                            org.apache.commons, javax.mail, org.apache.velocity, commons-logging
                        </includeGroupIds>
                        <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Move property files out of JAR and put in a directory say “target/properties-files” -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>${maven-antrun-plugin.version}</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <copy todir="target/properties-files" overwrite="true">
                                <fileset dir="src/main/java/pk/training/basitMahmood/util">
                                    <include name="*.properties"/>
                                </fileset> 
                                <fileset dir="src/main/resources/email">
                                    <include name="*.properties"/>
                                </fileset>    
                            </copy>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在我尝试运行程序时使用此配置后,我收到错误消息

IOException parsing XML document from class path resource 
[spring/app-context-xml.xml]; nested exception is 
java.io.FileNotFoundException: class path resource 
[spring/app-context-xml.xml] cannot be opened because it does not exist

有时我得到的错误是

java.lang.NoClassDefFoundError: pk/training/basitMahmood/BatchImport
Caused by: java.lang.ClassNotFoundException: pk.training.basitMahmood.BatchImport

虽然使用以前的配置(即,只有 jar 复制)一切正常

我做错了什么?我怎样才能实现我想要的?请帮忙

谢谢

4

1 回答 1

0

我前段时间遇到了同样的问题,我使用maven-shade-plugin解决了它。
我的样本:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <finalName>${final.package.name}</finalName>
        <artifactSet>
            <excludes>
            </excludes>
        </artifactSet>
        <filters>
            <filter>
                <artifact>*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                    <exclude>META-INF/*.INF</exclude>
                </excludes>
            </filter>
        </filters>
        <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/spring.schemas</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <manifestEntries>
                    <mainClass>mainClass</mainClass>
                    <Version>${project.version}</Version>
                    <Codename>codename</Codename>
                    <Build-Time>${maven.build.timestamp}</Build-Time>
                </manifestEntries>
            </transformer>
        </transformers>
    </configuration>
</plugin>
于 2013-08-01T14:08:13.340 回答