尝试使用maven将属性文件放在jar外时,程序未运行

时间:2013-08-01 13:40:53

标签: java eclipse spring maven-3 maven-plugin

我使用m2e在eclipse中有一个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复制),但一切正常

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

由于

1 个答案:

答案 0 :(得分: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>