8

I'm using the maven-antrun-plugin to do a bunch of work with Ant, which ultimately results in a zip file. I'd like to deploy the zip file to our maven server (Artifactory). The maven-antrun-portion works as intended and successfully creates the zip file; however deployment fails with the following error message:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.6:deploy (default-deploy) on project projectname: The packaging for this project did not assign a file to the build artifact

My POM file is as follows:

<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.company.division</groupId>
    <artifactId>projectname</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.company.product</groupId>
        <artifactId>parentproject</artifactId>
        <version>1.0.0</version>
    </parent>

    <distributionManagement>
        <snapshotRepository>
            <id>artifactory</id>
            <name>artifactory-snapshots</name>
            <url>http://localartifactoryserver/artifactory/libs-snapshot-local</url>
            <uniqueVersion>false</uniqueVersion>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <!-- Some dependencies... -->
    </dependencies>

    <build>
        <plugins>
            <!-- Compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF8</encoding>
                    <optimize>true</optimize>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <configuration>
                            <target>
                                <!-- Do lots of other stuff with Ant. -->

                                <!-- Create a zip file. -->
                                <zip basedir="mydir" destfile="${WORKSPACE}/MyZip.zip" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <version>${project.version}</version>
                    <packaging>zip</packaging>
                    <file>MyZip.zip</file>
                    <url>${project.distributionManagement.snapshotRepository.url}</url>
                </configuration>
              </plugin>
        </plugins>
    </build>
</project>

When I invoke this (from the parent POM) with mvn -U -pl projectname clean deploy I get the aforementioned error during the deploy phase. Does anyone know what I'm doing wrong or how I can fix this?

Justin Garrick
  • 14,767
  • 7
  • 41
  • 66

3 Answers3

12

The solution that worked for me (I'm not sure if it is ideal, it seems rather hackish) was to switch to the deploy:deploy-file goal:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.6</version>
    <goals>
        <goal>deploy-file</goal>
    </goals>
    <configuration>
        <repositoryId>artifactory</repositoryId>
        <packaging>zip</packaging>
        <generatePom>true</generatePom>
        <url>${project.distributionManagement.snapshotRepository.url}</url>
        <artifactId>${project.artifactId}</artifactId>
        <groupId>${project.groupId}</groupId>
        <version>${project.version}</version>
        <file>${WORKSPACE}/MyZip.zip</file>
    </configuration>
</plugin>

and invoke it explicitly:

mvn -U -X -pl projectname clean install deploy:deploy-file
carlspring
  • 31,231
  • 29
  • 115
  • 197
Justin Garrick
  • 14,767
  • 7
  • 41
  • 66
  • Assuming your project is `pom`, one has to be advised that the transitive dependencies of such a project will not be resolved (later on by other projects), if you deploy the artifact this way. – carlspring Mar 26 '13 at 16:03
  • A life-saver. I was having to do this on the command line. It's nice to know how to do it inside a pom.xml file. – Chris F Apr 30 '14 at 22:25
  • 2
    If you add an element and include deploy in an , the deployment will be included in the maven lifecycle. – John Nov 25 '15 at 14:31
  • I had a cryptic error by artifactory "IO error while trying to save resource : org.artifactory.api.repo.exception.maven.BadPomException: Failed to read POM for : only whitespace content allowed before start tag and not P (position: START_DOCUMENT seen P... @1:1) ." The problem was that packaging zip does not exist for pom.xml. So you must write explicitly zip and not take the pom value – fan Dec 04 '19 at 23:33
  • What do you do if you have both snapshots and releases defined for your repositoryId? You are using explicitely ${project.distributionManagement.snapshotRepository.url}, so you are forcing the upload only towards snaphsot repository. @fl4l solution has not this limitation. – Alessandro C May 01 '20 at 17:58
4

The solution worked for me is to add the <attachartifact> tag after zip creation, filled with the same path and zip filename . So something like:

    <executions>
        <execution>
            <id>zip-artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <zip destfile="${project.build.directory}/MyStuff-${project.version}.zip" update="true" comment="This is my comment...">
                        <fileset dir="${project.build.directory}/MyStuff" />
                    </zip>
                    <attachartifact file="${project.build.directory}/MyStuff-${project.version}.zip" type="zip" />
                </target>
            </configuration>
        </execution>
    </executions>

Remember that the zip file has to exists, otherwise attachartifact returns "file does not exists" error (consider to use whenempty="create" in tag in order to avoid errors).

fl4l
  • 1,580
  • 4
  • 21
  • 27
1

While looking for a way to add comment to a zip file I found this question. The deploy worked fine but had troubles with the maven release to post into nexus. The solution below solved my problem, I made an empty zip assembly and then simply replaced it with the zip file from the ant task which allowed me to add the comment to the zip file. This way the artifact is generated and not transitive.

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>${project.build.finalName}</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<fileSets>
    <fileSet>
        <directory>${project.build.directory}/MyStuff/emptydir</directory>
        <outputDirectory></outputDirectory>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
</fileSets>

        <plugin>
            <!--                 make an assembly (zip the LxBase) for the distribuition -->
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>docs-assembly</id>
                    <phase>package</phase>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/assemble.xml</descriptor>
                        </descriptors>
                    </configuration>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>zip-artifacts</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <zip destfile="${project.build.directory}/MyStuff-${project.version}.zip" update="true" comment="This is my comment...">
                                <fileset dir="${project.build.directory}/MyStuff" />
                            </zip>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
The CTO
  • 71
  • 3