java - 尝试在maven的pom.xml中添加插件

标签 java xml maven pom.xml

我正在尝试在 pom.xml 中添加此插件:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}-${project.version}-withDependency-ShadedForAndroid</finalName>
                            <artifactSet>
                                <includes>
                                    <include>com.mashape.unirest:unirest-java</include>
                                    <include>org.apache.httpcomponents:httpclient</include>
                                    <include>org.apache.httpcomponents:httpcore</include>
                                    <include>org.apache.httpcomponents:httpcore-nio</include>
                                    <include>org.apache.httpcomponents:httpasyncclient</include>
                                    <include>org.apache.httpcomponents:httpmime</include>
                                    <include>org.json:json</include>
                                    <include>commons-logging:commons-logging</include>
                                    <include>commons-codec:commons-codec</include>
                                </includes>
                            </artifactSet>
                            <relocations>
                                <relocation>
                                    <pattern>org.apache.http</pattern>
                                    <shadedPattern>com.mashape.relocation</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

在此 pom.xml 中:

<?xml version="1.0"?>
<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.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <packaging>jar</packaging>
    <version>1.4.10-SNAPSHOT</version>
    <name>unirest-java</name>
    <description>Simplified, lightweight HTTP client library</description>
    <url>http://unirest.io/</url>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>

    <licenses>
        <license>
            <name>MIT</name>
            <url>http://opensource.org/licenses/MIT</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <scm>
        <url>https://github.com/Mashape/unirest-java</url>
        <connection>scm:git:git@github.com:Mashape/unirest-java.git</connection>
        <developerConnection>scm:git:git@github.com:Mashape/unirest-java.git</developerConnection>
    </scm>

    <developers>
        <developer>
            <id>mashape</id>
            <name>Mashape</name>
            <email>opensource@mashape.com</email>
            <url>https://github.com/Mashape</url>
            <organization>Mashape</organization>
            <organizationUrl>https://www.mashape.com</organizationUrl>
        </developer>
    </developers>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <jackson.version>2.6.0</jackson.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <fork>true</fork>
                    <meminitial>128m</meminitial>
                    <maxmem>512m</maxmem>
                    <source>1.5</source>
                    <target>1.5</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160212</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

但我不知道该怎么做,如果有人可以解释一下;当我尝试时,我遇到了一些关于将其放在错误位置的问题。无法识别的标签“插件”.. 谢谢。

最佳答案

您在 POM 中已经定义了插件标签。您可以在其中添加另一个插件。查看示例;

<build>
 <plugins>
  <plugin>
  <!-- first plugin here -->
  </plugin>
  <plugin>
  <!-- second plugin here -->
  </plugin>
 </plugins>
</build>

在 POM 中包含提供的插件后,我已经完成了您的 POM

<?xml version="1.0"?>
<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.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<packaging>jar</packaging>
<version>1.4.10-SNAPSHOT</version>
<name>unirest-java</name>
<description>Simplified, lightweight HTTP client library</description>
<url>http://unirest.io/</url>

<parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>7</version>
</parent>

<licenses>
    <license>
        <name>MIT</name>
        <url>http://opensource.org/licenses/MIT</url>
        <distribution>repo</distribution>
    </license>
</licenses>

<scm>
    <url>https://github.com/Mashape/unirest-java</url>
    <connection>scm:git:git@github.com:Mashape/unirest-java.git</connection>
    <developerConnection>scm:git:git@github.com:Mashape/unirest-java.git</developerConnection>
</scm>

<developers>
    <developer>
        <id>mashape</id>
        <name>Mashape</name>
        <email>opensource@mashape.com</email>
        <url>https://github.com/Mashape</url>
        <organization>Mashape</organization>
        <organizationUrl>https://www.mashape.com</organizationUrl>
    </developer>
</developers>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <jackson.version>2.6.0</jackson.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <fork>true</fork>
                <meminitial>128m</meminitial>
                <maxmem>512m</maxmem>
                <source>1.5</source>
                <target>1.5</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.artifactId}-${project.version}-withDependency-ShadedForAndroid</finalName>
                        <artifactSet>
                            <includes>
                                <include>com.mashape.unirest:unirest-java</include>
                                <include>org.apache.httpcomponents:httpclient</include>
                                <include>org.apache.httpcomponents:httpcore</include>
                                <include>org.apache.httpcomponents:httpcore-nio</include>
                                <include>org.apache.httpcomponents:httpasyncclient</include>
                                <include>org.apache.httpcomponents:httpmime</include>
                                <include>org.json:json</include>
                                <include>commons-logging:commons-logging</include>
                                <include>commons-codec:commons-codec</include>
                            </includes>
                        </artifactSet>
                        <relocations>
                            <relocation>
                                <pattern>org.apache.http</pattern>
                                <shadedPattern>com.mashape.relocation</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160212</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>

关于java - 尝试在maven的pom.xml中添加插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41797213/

相关文章:

java - 哪个代码更有效?为什么?

Java : unable to marshal type "entities.Person" as an element because

javascript - Dymo JavaScript 打印条形码位置

xml - XmlBuilder.processing 可以接受更多参数吗?

java - 如何让 tomcat 看到 maven jar 而不是共享/lib 中的 jar ?

maven - PMD/CPD 无法检测重复代码

java - 使用一个参数在java中创建compareTo方法

java - 使用应用程序图标而不是默认文件夹图标创建文件夹

java - 无法从 DenseInstance 转换为 Instance

maven - 当文件存在时使用通配符激活配置文件