11

StackOverflow 上有一些看起来非常相似的问题,但我找不到任何有效的问题。

现在,我正在尝试从构建的 Jar 中删除 log4j。

虽然我是 Maven 的新手,而且我确信这一切都错了,但这就是我正在尝试的。

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.icf.onecpd</groupId>
    <artifactId>pdfgen</artifactId>
    <version>1.1</version>
    <packaging>jar</packaging>

    <name>PDF Generator</name>
    <url></url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.2.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <scm>
        <url>https://svn.tms.icfi.com/svn/HUD/onecpd/income_calculator_pdf_generator</url>
    </scm>
</project>

程序集.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <dependencySets>
        <dependencySet>
            <excludes>
                <exclude>log4j:log4j:*</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

组装:单个日志:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building PDF Generator 1.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (default-cli) @ pdfgen ---
[INFO] Reading assembly descriptor: assembly.xml
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] Building jar: /home/jamie/workspace_ruby2/onecpd_pdfgen/target/pdfgen-1.1-jar-with-dependencies.jar
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.368s
[INFO] Finished at: Wed Mar 20 13:45:33 EDT 2013
[INFO] Final Memory: 8M/158M
[INFO] ------------------------------------------------------------------------

但是,生成的 Jar 最终会包含 /org/apache/log4j/ 类。

我该如何摆脱它们?

谢谢!

4

1 回答 1

14

您可以尝试将依赖项声明为提供的范围,例如

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>provided</scope>
    </dependency>
于 2013-03-20T18:09:16.093 回答