有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我能强迫Maven在编译错误的情况下打包jar吗?

因此,我运行maven package将依赖项构建到jar中。但是,我需要Maven忽略任何编译错误并打包jar

这将如何实现?我的发言如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- Project specific stuff would be here... -->

    <build>
        <sourceDirectory>wherever</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- etc... -->
    </dependencies>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

搜索SO只向我展示了如何忽略单元测试中的编译错误。我只是想编译我的包,尽管代码中有任何错误


编辑:人们似乎在抨击我。我做Java已经5年了。我知道你不应该编译错误。我知道这意味着什么

我的老板和我一起编程。他特别说,“我可以在Eclipse中使用错误进行编译,所以在Maven中使用错误进行编译。”这不是因为我无知,也不是因为我拒绝接受我的错误。因为那是我被特别命令要做的。我没有问这个问题,因为我不称职,正如你们许多人所想的那样

你想恨我就恨我,只要知道你这样做是不公平的


共 (5) 个答案

  1. # 3 楼答案

    在@wings Answer上构建

    也可以在pom中使用failOnError=false标志

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <failOnError>false</failOnError>
        </configuration>
    </plugin>
    

    或者通过命令行

    mvn package -Dmaven.compiler.failOnError=false
    
  2. # 4 楼答案

    当你运行maven软件包时。这意味着maven将在“打包”阶段之前运行所有阶段。因此,maven compile肯定会被触发。由于编译错误,maven将在“编译”阶段停止

    即使有编译错误也要打包项目。我认为你应该使用“maven assembly plugin”来打包你的依赖项,插件的阶段应该在“编译”之前。例如:“过程来源”或“过程资源”

    这样,在maven编译项目的源代码之前,您将拥有一个包含所有依赖项的包。当然,maven将再次停留在“编译”阶段。但你已经得到了你想要的

    希望这有帮助

  3. # 5 楼答案

    如果只是关于依赖关系,那么^{}

    Goal that copies the project dependencies from the repository to a defined location.

    使用^{}user-defineddescriptor从上面定义的位置创建依赖项的JAR:

    Assemble an application bundle or distribution from an assembly descriptor.