通过Maven上传/下载整个目录到Nexus

13

是否可以将整个目录及其所有子目录上传/下载到Nexus存储库服务器中?


这是一个现有的Maven仓库还是一些任意的文件? - Sean Patrick Floyd
上传到 Nexus 服务器,这是用于任意文件的。 从服务器下载,我猜这将来自 Nexus 存储库。 - Peter
3个回答

5

如果您想要实际部署一个文件层次结构,我使用 GMaven(嵌入在maven中的groovy)构建了一个解决方案。

使用下面的pom文件,提供一些属性并点击mvn install。 文件夹将被爬取,并且其中所有的文件将使用相对路径中获取的artifactId进行部署。例如:

给定基础文件夹

c:\a\b\c

该文件

c:\a\b\c\def\ghi\jkl.mno

将具有artifactId def-ghi-jkl和打包mno,当然可以更改为其他内容。

存储库信息将从pom中获取,因此您需要在pom中提供distributionManagement元素。

这是它(其中许多代码取自deploy:deploy-file mojo):

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>folderdeployer</artifactId>
    <packaging>jar</packaging>
    <version>SNAPSHOT</version>

    <properties>
        <!-- This is where your artifacts are -->
        <deploy.basefolder>c:\temp\stuff</deploy.basefolder>

        <!-- This will be used as groupId -->
        <deploy.groupId>my.groupid</deploy.groupId>

        <!-- this will be used as version -->
        <deploy.version>1.2.3</deploy.version>
    </properties>
    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.groovy.maven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.0</version>
                <dependencies>
                    <dependency>
                        <groupId>commons-io</groupId>
                        <artifactId>commons-io</artifactId>
                        <version>1.4</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>deploy-files</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                            <![CDATA[
// read components from plexus container             
def layout = session.lookup(
    'org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout');
def repoFactory = session.lookup(
    'org.apache.maven.artifact.repository.ArtifactRepositoryFactory');
def repository = repoFactory.createDeploymentArtifactRepository(
    pom.distributionManagement.repository.id,
    pom.distributionManagement.repository.url,
    layout, true );
def localRepository = session.localRepository;
def helper =
    session.lookup("org.apache.maven.project.MavenProjectHelper");
def afm = session.lookup(
    'org.apache.maven.artifact.handler.manager.ArtifactHandlerManager');
def factory = new org.apache.maven.artifact.factory.DefaultArtifactFactory();
factory.class.getDeclaredField("artifactHandlerManager").accessible = true;
factory.artifactHandlerManager=afm;

def deployer = session.lookup(
    'org.apache.maven.artifact.deployer.ArtifactDeployer');

// initialize properties
def baseFolder = new File(pom.properties['deploy.basefolder']);
def groupId = pom.properties['deploy.groupId'];
def version = pom.properties['deploy.version'];

// iterate over all files recursively
baseFolder.eachFileRecurse{
    if(it.isDirectory())return;

    // packaging = file.extension
    def packaging = it.name.replaceAll( /.+\./ , '' );
    // artifactId = file.relativePath.replace '/' , '-'
    def artifactId = it.absolutePath
        .replace(baseFolder.absolutePath, '')
        .substring(1)
        .replaceFirst( /\..*?$/ , '')
        .replaceAll( /\W+/ , '-' );
    def artifact = 
        factory.createBuildArtifact( 
            groupId, artifactId, version, packaging );

    // create pom for artifact
    def model = new org.apache.maven.model.Model();
    model.setModelVersion( "4.0.0" );
    model.setGroupId( groupId );
    model.setArtifactId( artifactId );
    model.setVersion( version );
    model.setPackaging( packaging );
    File pomFile = File.createTempFile( "mvndeploy", ".pom" );
    pomFile.deleteOnExit();
    fw = org.codehaus.plexus.util.WriterFactory.newXmlWriter( pomFile );
    new org.apache.maven.model.io.xpp3.MavenXpp3Writer().write( fw, model );
    org.apache.commons.io.IOUtils.closeQuietly( fw );

    def metadata = 
        new org.apache.maven.project.artifact.ProjectArtifactMetadata(
                    artifact, pomFile );
    artifact.addMetadata( metadata );

    // deploy file
    deployer.deploy(it, artifact, repository, localRepository );
}
                                    ]]>
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>your repo id here</id>
            <url>scp://your.repo.url/here</url>
            <layout>default</layout>
        </repository>
    </distributionManagement>

</project>

编辑:

我在我的博客上详细阐述了这个问题。


有趣,我会研究一下。谢谢! - Peter
3
有帮助!我将其修改以适应本地代码库文件夹结构。https://gist.github.com/aleung/5194777 - aleung
3
我们使用了一种修改过的@aleung脚本版本已经将近一年,一直没有出现问题。用于处理'maven-metadata.xml'和'.sha'以及'.md5'文件的修改可以在派生的gist中找到:https://gist.github.com/jakub-bochenski/7802ee7f44b8e3b99bdd69b2ab150e6c - Jakub Bochenski

4

你可以将目录打包成zip文件并发送。此文件夹的用户可以从Nexus下载并使用dependency:unpack进行解压缩。


是的,这绝对是一种方法,但并不能完全满足要求。不过还是谢谢你的建议 =) - Peter

0

默认情况下不支持,但是您可以使用Nexus的API通过脚本上传。这是我的脚本,用于上传本地存储库文件夹中的内容。它使用pom文件提供的信息来上传构件。

这样可以节省很多时间,并且对我的用例非常有效。

#!/bin/bash

# this script needs a "artifacts" file right next to it.  Create it by using following script in your .m2-folder
#       find -iname "*.pom" -printf "%h\n" > files; find -iname "*.jar" -printf "%h\n" >> files; cat files | sort | uniq -u > artifacts; rm files

NEXUS_USERNAME="admin"
NEXUS_PASSWORD="nexus"
NEXUS_URL="localhost:8081"

cat artifacts | while read i; do

    pompath=$(find $i -name *.pom)
    jarpath=$(find $i -name *.jar)
    
    # extracting metainformations from pom
    groupId=$(echo $pompath | xargs xpath -e 'project/groupId/text()')
    artifactId=$(echo $pompath | xargs xpath -e 'project/artifactId/text()')
    version=$(echo $pompath | xargs xpath -e 'project/version/text()')
    if test -z "$groupId"
    then 
        echo "project-groupId is empty - using parent/groupId"
        groupId=$(echo $pompath | xargs xpath -e 'project/parent/groupId/text()')
    fi
    
    if test -z "$version"
    then 
        echo "project-version of jar-pom is empty - using parent/version"
        version=$(echo $pompath | xargs xpath -e 'project/parent/version/text()')
    fi


    # choosing upload-strategy, preferring jar-upload
    if test -z "$jarpath"
    then
        echo "uploading $artifactId as pom"
        # a 400 error means that the artifactId already exists
        mvn deploy:deploy-file \
         -DgroupId=$groupId \
         -DartifactId=$artifactId \
         -Dversion=$version \
         -Dpackaging=pom \
         -Dfile=$pompath \
         -Durl="http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_URL}/repository/maven-releases/"
        echo "uploading $pompath with groupId: $groupId; artifactId: $artifactId; version: $version"
    else 
        echo "uploading $artifactId as jar"
        # a 400 error means that the artifactId already exists
        mvn deploy:deploy-file \
         -DgroupId=$groupId \
         -DartifactId=$artifactId \
         -Dversion=$version \
         -Dpackaging=jar \
         -DgeneratePom=true \
         -Dfile=$jarpath \
         -Durl="http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_URL}/repository/maven-releases"      
        echo "uploading $jarpath with groupId: $groupId; artifactId: $artifactId; version: $version"
    fi 
  
done 

echo 'done uploading artifacts'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接