通过Maven上传/下载整个目录到Nexus [英] Upload/Download entire directory to Nexus through Maven

查看:138
本文介绍了通过Maven上传/下载整个目录到Nexus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

如果要实际部署文件层次结构,我使用 GMaven (groovy embedded in maven)。



使用下面的pom,提供几个属性,并点击 mvn install
将抓取该文件夹,并使用从相对路径获取的artifactId来部署文件夹中的所有文件。例如



给定基本文件夹

  c:\a\\ \\ b \c 

文件

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

将具有artifactId def-ghi-jkl 和包装 mno ,这当然可以改为别的东西。



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

这是(很多这个代码来自于 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>
<! - 这是您的工件是 - >
< deploy.basefolder> c:\temp\stuff< /deploy.basefolder>

<! - 这将被用作groupId - >
< deploy.groupId> my.groupid< /deploy.groupId>

<! - 这将被用作版本 - >
< 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>
<依赖关系>
<依赖关系>
< groupId> commons-io< / groupId>
< artifactId> commons-io< / artifactId>
< version> 1.4< / version>
< / dependency>
< / dependencies>
<执行>
< execution>
< id> deploy-files< / id>
< phase> prepare-package< / phase>
< goals>
< goal> execute< / goal>
< / goals>
< configuration>
< source>
<![CDATA [
//从plexus容器中读取组件
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');

//初始化属性
def baseFolder = new File(pom.properties ['deploy.basefolder']);
def groupId = pom.properties ['deploy.groupId'];
def version = pom.properties ['deploy.version'];

//递归迭代所有文件
baseFolder.eachFileRecurse {
if(it.isDirectory())return;

// package = 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);

//为工件创建pom
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);
文件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(元数据);

//部署文件
deployer.deploy(it,art​​ifact,repository,localRepository);
}
]]>
< / source>
< / configuration>
< / execution>
< / executions>
< / plugin>
< / plugins>
< / build>

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

< / project>






编辑:



我详细阐述了这个我的博客


Is it possible to upload/download an entire directory and all of the sub-directories within it to/from a Nexus repository server?

解决方案

In case you want to actually deploy a hierarchy of files, I hacked together a solution using GMaven (groovy embedded in maven).

Use the pom below, supply a few properties and hit mvn install. The folder will be crawled and all files inside it will be deployed using an artifactId taken from the relative path. e.g.

Given the base folder

c:\a\b\c

the file

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

would have the artifactId def-ghi-jkl and the packaging mno, this can of course be changed to something else.

The repository info will be taken from the pom, so you need to supply a distributionManagement element in the pom.

Here it is (a lot of this code is taken from the 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>


EDIT:

I elaborated on this on my blog

这篇关于通过Maven上传/下载整个目录到Nexus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆