1

私はm2eを使用してEclipseでMavenプロジェクトを持っています。春を訴えます。Maven が jar ファイルをビルドすると、すべての依存関係の jar ファイルが依存関係の jar ファイルフォルダーにコピーされます。ここで私はそれをやっています。

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

物事がうまくいくまで。プロパティファイルを変更した後に毎回jarを作成する必要がないため、JARに含めたくない2つのプロパティファイルがあります。プロパティ ファイルが jar の外にあり、それらのエントリがマニフェストに追加され、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 に答える