0

I want to configure a manual file deployment to a remote repository using either the snapshot repository (using configured property project.distributionManagement.snapshotRepository.url) if the current version of the project is a snapshot version or to the release repository (using configured property project.distributionManagement.repository.url) otherwise.

I want to deploy a swagger json schema to the repository and I did not find any way other than a manual deployment.

4

3 回答 3

2

There is a work around using builder helper bsh to use the correct repository from the distribution management configuration. It sets a property with the correct value. Then the maven deploy plugin is called with the goal deploy-file and this URL.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.12</version>
    <executions>
        <execution>
            <goals>
                <goal>bsh-property</goal>
            </goals>
            <configuration>
                <properties>
                    <property>deploy.url</property>
                </properties>
                <source>deploy.url = project.getVersion().endsWith("-SNAPSHOT") ? project.getDistributionManagement().getSnapshotRepository().getUrl() : project.getDistributionManagement().getRepository().getUrl() ;</source>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <phase>deploy</phase>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <url>${deploy.url}</url>
                <repositoryId>releases</repositoryId>
                <file>${swagger.directory}/swagger.json</file>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <version>${project.version}</version>
                <packaging>json</packaging>
                <classifier>swagger</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>
于 2016-12-16T09:29:11.777 回答
0

If your need is to deploy some artifact (maybe a json text file) that has not a corresponding pom.xml file supporting it, you may use the deploy-file target:

http://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html

This allows you to deploy any kind of artifact from any origin into a maven repository, specifying by command line the group, artifact and version coordinates, and anything else you need.

In order to deploy it to the right place depending on your version, I can propose you this fragment of an ant script we built for a similar purpose:

<condition property="url" value="${snapshots.repo.url}" else="${releases.repo.url}">
            <contains string="${project.version}" substring="-SNAPSHOT"/>
        </condition>
        <exec executable="cmd">
            <arg value="/c"/>
            <arg value="mvn.bat"/>
            <arg value="deploy:deploy-file"/> 
            <arg value="-DgroupId=com.myorg.swagger"/>
            <arg value="-DartifactId=swagger_file"/>
            <arg value="-Dversion=${project.version}"/>
            <arg value="-U"/>
            <arg value="-Dfile=./mydir/my_swagger_file.json"/>
            <arg value="-Durl=${url}"/>
            <arg value="-DrepositoryId=my_repo_id"/>
          </exec>

This will only work in Windows, but is easily adaptable for any other OS. The interesting bit here is the repositoryId, that should point to an existing authentication in your settings.xml:

...
<servers>
        <server>
            <id>my_repo_id</id>
            <username>your_user_for_deployment</username>
            <password>your_pwd_for_deployment</password>
        </server>
</servers>
...

Hope this will help you ^^

于 2016-12-16T10:00:00.140 回答
0

@Nicolas Henneaux answer is correct but deploy phase in build-helper-maven-plugin is incorrect. New properties are visible in next stage of lifecycle so in this case it won't be visible. After removing it (the defeault is validate) everything started to work.

于 2020-10-14T20:17:26.113 回答