代码之家  ›  专栏  ›  技术社区  ›  codependent

部署在Nexus上的自定义Maven插件不能用于本地计算机上的项目,除非该插件安装在本地repo上或设置为依赖项

  •  0
  • codependent  · 技术社区  · 7 年前

    我开发了一个定制的Maven插件,并将其部署在远程Nexus存储库上(mvn部署)。jar已正确上载pom,如下所示:

    <?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">     <modelVersion>4.0.0</modelVersion>
    
        <groupId>something</groupId>    
        <artifactId>xxx-maven-plugin</artifactId>
        <version>1.0.0-SNAPSHOT</version>   
        <packaging>maven-plugin</packaging>
    

    jar包含必要的插件。xml文件,使其看起来很好。

    我已配置一个项目以使用该插件:

        <plugins>
            <plugin>
                <groupId>something</groupId>
                <artifactId>xxx-maven-plugin</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </plugin>
            ...
    

    但当我跑的时候 mvn xxx:mygoal 显示此错误:

    [ERROR] No plugin found for prefix 'xxx' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/myuser/.m2/repository), remote-repo (http://someremoterepo/nexus/content/groups/central/)] -> [Help 1]
    

    唯一可行的方法是将插件作为插件和依赖项添加到项目中:

        ...
            <dependency>
                <groupId>something</groupId>
                <artifactId>xxx-maven-plugin</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
        <plugins>
            <plugin>
                <groupId>something</groupId>
                <artifactId>xxx-maven-plugin</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </plugin>
            ...
    

    现在它确实找到了插件并正确执行它。我做错了什么?我想应该没有必要将它同时定义为插件和依赖项,其他第三方插件没有这个要求。

    更新:

    我已将此添加到我的设置中。xml文件:

         <pluginGroup>something</pluginGroup>
      </pluginGroups>
    

    现在错误不同了:

    [WARNING] The POM for something:xxx-maven-plugin:jar:1.0.0-SNAPSHOT is missing, no dependency information available
    [WARNING] Failed to retrieve plugin descriptor for something:xxx-maven-plugin:1.0.0-SNAPSHOT: Plugin something:xxx-maven-plugin:1.0.0-SNAPSHOT or one of its dependencies could not be resolved: Could not find artifact something:xxx-maven-plugin:jar:1.0.0-SNAPSHOT
    [WARNING] The POM for something:xxx-maven-plugin:jar:1.0.0-SNAPSHOT is missing, no dependency information available
    ...
    [ERROR] Plugin something:xxx-maven-plugin:1.0.0-SNAPSHOT or one of its dependencies could not be resolved: Could not find artifact something:xxx-maven-plugin:jar:1.0.0-SNAPSHOT -> [Help 1]
    

    它实际上是在回购中。同样,如果我显式地将其添加为依赖项,它也会起作用???

    更新2:

    使用Nexus GAV搜索界面,我寻找一个带有artifactId spring boot maven插件和打包maven插件的插件,它找到了它。使用包装罐,它也可以找到它。

    然后我对我的插件也做了同样的事情,在寻找打包maven插件时,它会找到它,但在使用打包jar时却找不到。

    更新3:

    在我的本地repo中,有一个maven元数据中心镜像。包含以下内容的xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <metadata>
      <plugins>
        <plugin>
          <name>something</name>
          <prefix>xxx</prefix>
          <artifactId>xxx-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </metadata>
    

    完整配置

    本地设置。xml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
       <localRepository>/Users/myuser/.m2/repository</localRepository>
    
      <pluginGroups>
         <pluginGroup>something</pluginGroup>
      </pluginGroups>
    
      <proxies>
      </proxies>
    
      <servers>
         <server>
           <id>someremoterepo</id>
           <username>xxxx</username>
           <password>yyyy</password>
         </server>
      </servers>
    
      <mirrors>
         <mirror>
           <id>someremoterepo</id>
           <name>Nexus Repo</name>
           <url>http://someremoterepo/nexus/content/groups/central/</url>
           <mirrorOf>*</mirrorOf>
         </mirror>
      </mirrors>
    
      <profiles>
         <profile>
           <id>myprofile</id>
           <activation>
           <activeByDefault>true</activeByDefault>
           </activation>
           <repositories>
             <repository>
               <id>central</id>
               <name>Central</name>
               <releases>
               <enabled>true</enabled>
               <checksumPolicy>warn</checksumPolicy>
               </releases>
               <snapshots>
               <enabled>true</enabled>
               <updatePolicy>never</updatePolicy>
               <checksumPolicy>fail</checksumPolicy>
               </snapshots>
               <url>http://someremoterepo/nexus/content/groups/central</url>
             </repository>
           </repositories>
         </profile>
      </profiles>
    </settings>
    

    自定义插件pom。xml:

    <?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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>something</groupId>
        <artifactId>xxx-maven-plugin</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>maven-plugin</packaging>
    
        <name>xxx-maven-plugin</name>
        <description>xxx</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <kotlin.version>1.2.21</kotlin.version>
            <junit5.version>5.0.3</junit5.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-project</artifactId>
                <version>2.2.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-plugin-api</artifactId>
                <version>3.3.9</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven.plugin-tools</groupId>
                <artifactId>maven-plugin-annotations</artifactId>
                <version>3.5.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib-jdk8</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-reflect</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
    
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit5.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>1.5.10.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.github.sbrannen</groupId>
                <artifactId>spring-test-junit5</artifactId>
                <version>1.0.2</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
            <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.0</version>
                    <executions>
                        <execution>
                            <id>jacoco-initialize</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <version>${kotlin.version}</version>
                    <configuration>
                        <args>
                            <arg>-Xjsr305=strict</arg>
                        </args>
                        <compilerPlugins>
                            <plugin>spring</plugin>
                        </compilerPlugins>
                        <jvmTarget>1.8</jvmTarget>
                    </configuration>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>test-compile</id>
                            <phase>test-compile</phase>
                            <goals>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-allopen</artifactId>
                            <version>${kotlin.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    
        <distributionManagement>
            <repository>
                <id>someremoterepo-releases</id>
                <name>releases</name>
                <url>http://someremoterepo/nexus/content/repositories/releases/</url>
            </repository>
            <snapshotRepository>
                <id>someremoterepo-snapshots</id>
                <name>snapshots</name>
                <url>http://someremoterepo/nexus/content/repositories/snapshots/</url>
            </snapshotRepository>
        </distributionManagement>
    </project>
    

    包含插件的项目:

    唯一相关的位是:

    <?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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>agroup</groupId>
        <artifactId>some-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        ...
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.10.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        ...
        <plugins>
            <plugin>
                <groupId>something</groupId>
                <artifactId>xxx-maven-plugin</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        ...
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   codependent    7 年前

    经过很多努力,我终于解决了这个问题,解决方案是 pluginRepositories settings.xml 文件现在,它可以正确解析插件,而无需将其作为依赖项添加到项目pom中:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
       <localRepository>/Users/myuser/.m2/repository</localRepository>
    
      <pluginGroups>
         <pluginGroup>something</pluginGroup>
      </pluginGroups>
    
      <proxies>
      </proxies>
    
      <servers>
         <server>
           <id>someremoterepo</id>
           <username>xxxx</username>
           <password>yyyy</password>
         </server>
      </servers>
    
      <mirrors>
         <mirror>
           <id>someremoterepo</id>
           <name>Nexus Repo</name>
           <url>http://someremoterepo/nexus/content/groups/central/</url>
           <mirrorOf>*</mirrorOf>
         </mirror>
      </mirrors>
    
      <profiles>
         <profile>
           <id>myprofile</id>
           <activation>
           <activeByDefault>true</activeByDefault>
           </activation>
           <repositories>
             <repository>
               <id>central</id>
               <name>Central</name>
               <releases>
               <enabled>true</enabled>
               <checksumPolicy>warn</checksumPolicy>
               </releases>
               <snapshots>
               <enabled>true</enabled>
               <updatePolicy>never</updatePolicy>
               <checksumPolicy>fail</checksumPolicy>
               </snapshots>
               <url>http://someremoterepo/nexus/content/groups/central</url>
             </repository>
           </repositories>
           <pluginRepositories>
             <pluginRepository>
               <id>central</id>
               <name>Central</name>
               <releases>
                 <enabled>true</enabled>
                 <checksumPolicy>warn</checksumPolicy>
               </releases>
               <snapshots>
                 <enabled>true</enabled>
                 <updatePolicy>never</updatePolicy>
                 <checksumPolicy>fail</checksumPolicy>
               </snapshots>
               <url>http://someremoterepo/nexus/content/groups/central</url>
             </pluginRepository>
           </pluginRepositories>
         </profile>
      </profiles>
    </settings>