Including an unpacked War in the Assembly

为君一笑 提交于 2019-12-05 06:07:06

I know I should be able to add the unpacked war into my assembly. I see that unpack option, and I know it works for other dependencies. However, it looks like I can only access it via a <dependencySet> or a <moduleSet>. I should be able to specify my project as its own module. There must be something I am doing wrong.


Spleen Vent

This is the big thing I hate about Maven: Maven does a great job hiding things from you which is nice because it prevents you from doing stuff you shouldn't. I hate it when developers build Ant build.xml files because most developers don't understand how to do a build, and the build.xml becomes an unreadable mess. With Maven, this isn't an issue. Just configure your project, and Maven will take care of this for you.

But sometimes Maven is like a black box with a bunch of levers and buttons. You sit there pushing and pulling levers and buttons trying to figure out how to get it to do something you want to do. I spend way too much of my time trying to help developers to configure their Maven projects. They want to do something a little different like use hibernate or build source from WSDL files, and have no idea how to get Maven to do what they want.

One developer describes it as a self driving car which can't quite go where you want. You may even see the destination out the window, but you can't figure out how to manipulate the car's destination to get you there.

What I want to do should be easy. I just want to create an assembly, and instead of using the packed war file, I want it unpacked.

In Ant, this can be accomplished in a single task. In Maven, it's a mysterious process. I think I'm close, I am probably missing one little configuration parameter that will make it all work, but I've already spent hours working on this.


What I ended up doing

I used the maven-dependency-plugin to unpack my war. I wasn't sure whether this would work because I didn't want the dependency plugin downloading the war, but it seems to understand that when I specify my war, I am talking about the current build, and nothing is downloaded. (I don't even have the war in our Maven repo).

I also had to upgrade Maven from 2.x to 3.x because I need to make sure that the maven-dependency-plugin ran before the maven-assembly-plugin. Since both run in the packaging phase of the build, Maven 2.x can't guarantee the order the plugins run in.

Once I used that plugin, all I had to do was specify the directory where I unpacked the war in my assembly plugin, and it was included in my zip.

I still want to know how to use the <unpack> entity in the assembly plugin itself instead of having to use another plugin to unpack my war. If anyone can tell me how to do the unpack in the assembly file itself, I'll mark that as the correct answer.

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/unwar/${project.artifactId}</outputDirectory>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <type>war</type>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.2</version>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <outputDirectory>${project.build.directory}/archive</outputDirectory>
        <descriptors>
            <descriptor>src/assembly/bin.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

bin.xml (My Assembly)

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/assembly/scripts</directory>
            <fileMode>0755</fileMode>
            <lineEnding>lf</lineEnding>
            <includes>
                <include>deploy.sh</include>
                <include>lock_build.sh</include>
                <include>description.sh</include>
                <include>url-encode.pl</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/unwar</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

The ACTUAL Answer

Thanks to khmarbaise's link (see the comment below), I copied that project's assembly plugin and it almost worked. Like my attempt, it unpacked all the runtime jars, and not just the one I wanted. However, it also unpacked my war too.

There were only two differences between that link's answer and my attempt:

  • They included <useProjectArtifact>true</useProjectArtifact> and I didn't. However, this defaults to true. Removing it still allowed it to work.
  • They included a / in front of the directory name in the <outputDirectory>. Removing this made no difference.

This meant that it now matched what I had previously tried. So, why was it working this time.

Turns out, I was testing the assembly changes by simply running mvn assembly:single. After all, why do the whole build and repackage when I am simply trying to get the assembly to work. When you run just mvn assembly:single -- even though everything is already packaged, you get this error:

[WARNING] Cannot include project artifact: \
      com.vegicorp:myproj:war:1.0.0; \
      it doesn't have an associated file or directory.

And your war isn't unpacked. However, if you put the assembly into the package phase, and then run mvn package, everything works out just nifty.

I then spent time trying to just get my war and not all the associated runtime stuff with it. I used <includes/> to do this, but because I have a war and not a jar, I had to include a classifier in my <include>.

At last, I have everything working. I have this in my assembly:

<dependencySets>
    <dependencySet>
        <outputDirectory>${project.artifactId}</outputDirectory>
        <unpack>true</unpack>
        <scope>runtime</scope>
        <includes>
            <include>${project.groupId}:${project.artifactId}:*:${project.version}</include>
        </includes>
    </dependencySet>
</dependencySets>

And as long as I run mvn package, it works.

This is way better than what I had with the maven-dependency-plugin. Now, all of the information having to do with the assembly is in the assembly XML file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!