You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Keith Beattie <ks...@gmail.com> on 2007/06/23 02:38:03 UTC

Q: Multiple executable jars from a single project

Hello all,

I've got a project from which I'd like to create multiple 'executable' jar
files, which will differ only in the 'Main-Class: ' value of their manifest
files.  (These jars don't need their deps, I'm collecting all deps via a
parent pom with the assembly plugin.)

I realize that this violates somewhat the 'one artifact per project'
philosophy behind Maven, but short of refactoring the project into multiple
projects (most correct solution), or writing a launching class which starts
the proper class based on a passed in arg (2nd best solution), I thought I'd
ask here if this is possible to do in way a reasonably straightforward way
(simpler than the above two options).

At first, I thought I could use the jar plugin with multiple execution
elements (with the differing mainClass entry in each one) but haven't gotten
that to result in multiple jar files.  Another idea is to use the assembly
plugin with multiple descriptors but that doesn't allow for different
manifest/mainClass values.  Other, yet to be tried ideas are: multiple
profiles, or somehow aggregating poms together but that's new ground for me.

Any pointers, suggestions, hints, etc. ?

TIA,
ksb

Re: Q: Multiple executable jars from a single project

Posted by Tim Kettler <ti...@udo.edu>.
Keith,

I'm not that familiar with the assembly plugin but looking at the 
assembly descriptor reference i found the <attachmentClassifier/> tag,
for which the description says:

"When specified, the attachmentClassifier will cause the assembler to 
look at artifacts attached to the module instead of the main project 
artifact. If it can find an attached artifact matching the specified 
classifier, it will use it; otherwise, it will throw an exception."

Perhaps this does what you want. If this doesn't help you probably 
should ask the question in a new thread again so that someone other than 
me can step in.

-Tim

Keith Beattie schrieb:
> On 6/23/07, Tim Kettler <ti...@udo.edu> wrote:
>>
>> Hi,
>>
>> this should do what you want:
> 
> 
> [... very helpful jar-plugin example ...]
> 
> Excellent, this works.  Thank you very much!
> 
> You then probably also want to attach the second artifact to the pom
>> with the build-helper plugin's [1] attach-artifact goal so that it gets
>> installed and deployed.
> 
> 
> Indeed.  But (if I can follow up on this here) I seem to be having trouble
> getting the proper incantation between the build-helper and the assembly
> descriptor in the top-level pom.  At the top, my assembly descriptor is:
> 
> <assembly>
>  <id>dist</id>
>  <formats>
>    <format>dir</format>
>  </formats>
>  <includeBaseDirectory>false</includeBaseDirectory>
>  <moduleSets>
>    <moduleSet>
>      <includes>
>        <include>edu.wisc.icecube:StringHub</include>
>        <include>edu.wisc.icecube:secondaryBuilders</include>
>        <include> edu.wisc.icecube:eventBuilder-prod</include>
>        <include>edu.wisc.icecube:trigger</include>
>      </includes>
>      <binaries>
>        <outputDirectory></outputDirectory>
>        <unpack>false</unpack>
>      </binaries>
>    </moduleSet>
>  </moduleSets>
> </assembly>
> 
> In the project with the multiple jars  (the 'trigger' project) I have (in
> addition to the jar-plugin solution above) for build-helper:
> 
>      <plugin>
>        <groupId>org.codehaus.mojo</groupId>
>        <artifactId>build-helper-maven-plugin</artifactId>
>        <executions>
>          <execution>
>            <id>attach-artifacts</id>
>            <phase>package</phase>
>            <goals>
>              <goal>attach-artifact</goal>
>            </goals>
>            <configuration>
>              <artifacts>
>                <artifact>
>                  <classifier>gtrig</classifier>
>                  <file>${project.build.directory}/${artifactId}-${version}-
> gtrig.jar</file>
>                  <type>jar</type>
>                </artifact>
>                <artifact>
>                  <classifier>ittrig</classifier>
>                  <file>${project.build.directory}/${artifactId}-${version}-
> ittrig.jar</file>
>                  <type>jar</type>
>                </artifact>
>                <artifact>
>                  <classifier>iitrig</classifier>
>                  <file>${project.build.directory}/${artifactId}-${version}-
> iitrig.jar</file>
>                  <type>jar</type>
>                </artifact>
>              </artifacts>
>            </configuration>
>          </execution>
>        </executions>
>      </plugin>
> 
> Where there is a one-to-one correspondence between the jar executions and
> builder-helper artifacts.  This appears to work, based on output from 
> mvn -X
> within that trigger project, but then at the top a 'mvn -X clean package
> assembly:assembly' ends with:
> 
> [INFO] [assembly:assembly]
> [INFO] Reading assembly descriptor:
> /home/ksb/Projects/IceCube/pDAQ_Assemble_1326/src/assemble/dist.xml
> [DEBUG] No dependency sets specified.
> [DEBUG] Processing binary artifact for module project:
> edu.wisc.icecube:trigger:jar:1.0.0-SNAPSHOT
> [INFO]
> ------------------------------------------------------------------------
> [ERROR] BUILD ERROR
> [INFO]
> ------------------------------------------------------------------------
> [INFO] Failed to create assembly: Error adding file '
> edu.wisc.icecube:trigger:jar:1.0.0-SNAPSHOT' to archive:
> /home/ksb/Projects/IceCube/pDAQ_Assemble_1326/trigger/target/classes 
> isn't a
> file.
> 
> [INFO]
> ------------------------------------------------------------------------
> [DEBUG] Trace
> 
> It seems that either the build-helper isn't attaching the artifacts or the
> assembly isn't finding them properly, or both.  Ideas?
> 
> TIA and pointers to more complete docs on how to learn about configuring
> these plugins welcomed...
> 
> ksb
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Q: Multiple executable jars from a single project

Posted by Keith Beattie <ks...@gmail.com>.
On 6/23/07, Tim Kettler <ti...@udo.edu> wrote:
>
> Hi,
>
> this should do what you want:


[... very helpful jar-plugin example ...]

Excellent, this works.  Thank you very much!

You then probably also want to attach the second artifact to the pom
> with the build-helper plugin's [1] attach-artifact goal so that it gets
> installed and deployed.


Indeed.  But (if I can follow up on this here) I seem to be having trouble
getting the proper incantation between the build-helper and the assembly
descriptor in the top-level pom.  At the top, my assembly descriptor is:

<assembly>
  <id>dist</id>
  <formats>
    <format>dir</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <moduleSets>
    <moduleSet>
      <includes>
        <include>edu.wisc.icecube:StringHub</include>
        <include>edu.wisc.icecube:secondaryBuilders</include>
        <include> edu.wisc.icecube:eventBuilder-prod</include>
        <include>edu.wisc.icecube:trigger</include>
      </includes>
      <binaries>
        <outputDirectory></outputDirectory>
        <unpack>false</unpack>
      </binaries>
    </moduleSet>
  </moduleSets>
</assembly>

In the project with the multiple jars  (the 'trigger' project) I have (in
addition to the jar-plugin solution above) for build-helper:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <classifier>gtrig</classifier>
                  <file>${project.build.directory}/${artifactId}-${version}-
gtrig.jar</file>
                  <type>jar</type>
                </artifact>
                <artifact>
                  <classifier>ittrig</classifier>
                  <file>${project.build.directory}/${artifactId}-${version}-
ittrig.jar</file>
                  <type>jar</type>
                </artifact>
                <artifact>
                  <classifier>iitrig</classifier>
                  <file>${project.build.directory}/${artifactId}-${version}-
iitrig.jar</file>
                  <type>jar</type>
                </artifact>
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>

Where there is a one-to-one correspondence between the jar executions and
builder-helper artifacts.  This appears to work, based on output from mvn -X
within that trigger project, but then at the top a 'mvn -X clean package
assembly:assembly' ends with:

[INFO] [assembly:assembly]
[INFO] Reading assembly descriptor:
/home/ksb/Projects/IceCube/pDAQ_Assemble_1326/src/assemble/dist.xml
[DEBUG] No dependency sets specified.
[DEBUG] Processing binary artifact for module project:
edu.wisc.icecube:trigger:jar:1.0.0-SNAPSHOT
[INFO]
------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO]
------------------------------------------------------------------------
[INFO] Failed to create assembly: Error adding file '
edu.wisc.icecube:trigger:jar:1.0.0-SNAPSHOT' to archive:
/home/ksb/Projects/IceCube/pDAQ_Assemble_1326/trigger/target/classes isn't a
file.

[INFO]
------------------------------------------------------------------------
[DEBUG] Trace

It seems that either the build-helper isn't attaching the artifacts or the
assembly isn't finding them properly, or both.  Ideas?

TIA and pointers to more complete docs on how to learn about configuring
these plugins welcomed...

ksb

Re: Q: Multiple executable jars from a single project

Posted by Tim Kettler <ti...@udo.edu>.
Hi,

this should do what you want:

   <project>
     <modelVersion>4.0.0</modelVersion>

     <groupId>mygroup</groupId>
     <artifactId>myartifact</artifactId>
     <version>0.1-SNAPSHOT</version>

     <build>
       <plugins>
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-jar-plugin</artifactId>
           <configuration>
             <classifier>proga</classifier>
             <archive>
               <manifest>
                 <mainClass>fully.qualified.MainClassA</mainClass>
               </manifest>
             </archive>
           </configuration>
           <executions>
             <execution>
               <id>second-artifact</id>
               <goals>
                 <goal>jar</goal>
               </goals>
               <configuration>
                 <classifier>progb</classifier>
                 <archive>
                   <manifest>
                     <mainClass>fully.qualified.MainClassB</mainClass>
                   </manifest>
                 </archive>
               </configuration>
             </execution>
           </executions>
         </plugin>
       </plugins>
     </build>
   </project>

You then probably also want to attach the second artifact to the pom 
with the build-helper plugin's [1] attach-artifact goal so that it gets 
installed and deployed.

HTH
-Tim

[1] http://mojo.codehaus.org/build-helper-maven-plugin/

Keith Beattie schrieb:
> Hello all,
> 
> I've got a project from which I'd like to create multiple 'executable' jar
> files, which will differ only in the 'Main-Class: ' value of their manifest
> files.  (These jars don't need their deps, I'm collecting all deps via a
> parent pom with the assembly plugin.)
> 
> I realize that this violates somewhat the 'one artifact per project'
> philosophy behind Maven, but short of refactoring the project into multiple
> projects (most correct solution), or writing a launching class which starts
> the proper class based on a passed in arg (2nd best solution), I thought 
> I'd
> ask here if this is possible to do in way a reasonably straightforward way
> (simpler than the above two options).
> 
> At first, I thought I could use the jar plugin with multiple execution
> elements (with the differing mainClass entry in each one) but haven't 
> gotten
> that to result in multiple jar files.  Another idea is to use the assembly
> plugin with multiple descriptors but that doesn't allow for different
> manifest/mainClass values.  Other, yet to be tried ideas are: multiple
> profiles, or somehow aggregating poms together but that's new ground for 
> me.
> 
> Any pointers, suggestions, hints, etc. ?
> 
> TIA,
> ksb
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org