tags:

views:

469

answers:

4

Hi All My project uses many assemblies and hence i m interested only in the assemblies. On executing mvn install apart from the assemblies , i m getting the default packaged jar How can i avoid this ?

+1  A: 

I can read your question two ways, I've outlined answers for both below. If neither is correct, can you modify your question with a bit more explanation please.

1) Do you mean you have a project with default (jar) packaging, and you want to avoid the creation of the jar when no assembly is defined? If this is the case, what is the build achieving if no assembly is defined?

2) Do you instead mean that you are running mvn assembly:assembly to generate the assembly and want to know how to get that assembly when running the install goal?


For option 2, you can bind the assembly-plugin to a lifecycle phase to ensure it is always run, if you specify that <appendAssemblyId> should be false, then the assembly will replace the default jar.

For example, this configuration will invoke the assembly plugin during the packaging phase and replace the default jar:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-2</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
          <descriptor>src/main/assembly/archive.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>

For option 1, this is actually quite tricky to do. The best I can think of is to specify that the project has pom packaging and configure the project with the executions normally bound to the jar lifecycle in a profile. The lifecycle bindings you'd need to configure are listed in the introduction to the build lifecycle

Rich Seller
A: 

I have a pom.xml similar to the one you have provided. On executing mvn install , i m getting App1.jar , App2.jar and snapshot jar containing all contents

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-2</version>
            <executions>
                <execution>
                    <id>assemblyone</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>App1</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>${basedir}/src/main/resources/assemblies/report.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
                <execution>
                    <id>assemblytwo</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>App2</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>${basedir}/src/main/resources/assemblies/src.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Please let me know how i can avoid this snapshot(not sure of the exact term) jar and ensure that only assemblies are created

please merge this answer with your original question
sebastiangeiger
A: 

I'm not sure that you can really do that in a really simple way.

A solution is to call the clean plugin once the build is achieved, by doing that:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <executions>
                <execution>
                    <id>auto-clean</id>
                    <phase>package</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    ...
</build>

This way, the JAR created in the target/ directory will be deleted at the end of the Maven2 execution.

However, you will have to define another directory to store the assemblies created by Maven2. Otherwise, it will be deleted by the call of the clean plugin... If you want to store them in the directory assemblies-target/, you can add that in the pom.xml file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <configuration>
        ...
        <!-- Copy the ZIP in target/ of the ROOT directory. -->
        <outputDirectory>assemblies-target</outputDirectory>
    </configuration>
    ...
romaintaz
A: 

I think it would be much more clear if you showed us your whole POM and the artifacts that are being built. I can only guess as to what the problem is because your terminology is not what I am familiar with. This is my guess as to the problem: you have a POM configured to generated two assembly JARs, but you get a third JAR for the POM itself. In other words, if your POM's artifactId is MyApp, you are getting a MyApp-1.0.0.jar or similar in addition to the two JARs you actually want.

If that is the case, the problem boils down to that you are using Maven to create multiple artifacts from a single module. Maven is designed to produce only one primary artifact from each module. What I would do is change your POM to have a packaging type of "pom" and give it two modules in a <modules> section named App1 and App2. Create sub-directories under your module, one for each App. Give them each a POM configured for a single assembly, with a packaging type of "jar". Move the code/files/etc. as appropriate into each sub-module so there aren't any remaining in the parent module.

With Maven, if you find yourself generating two artifacts from one module (which you are), you should first consider that you are probably violating a Maven best-practice and rearrange things so you only produce one artifact per module.

Please let me know if this doesn't make sense and I will try to clarify.

SingleShot
Hi Thanks for the inputs.i don't think i would be able to create separate modules say App1, App2..bcoz my assembly jars namely App1.jar , App2.jar have common files to be included.if i specify the packagins as 'pom' , the MyApp-1.0.0 jar will not be created?
Well, those common files would be placed in a 3rd location, either in their own assembly that gets unpacked into each module, or (less clean) in the parent module and referenced by each child. Marking you parent module a "pom" will not create the JAR, however, you would still be violating Maven principles without doing what I suggest. Search on "Maven: The Definitive Guide" - a very nice free book on Maven.
SingleShot