0

Is it possible to build a jar for every java package in a project?

e.g. my packages are com.mypackage.foo, com.mypackage.bar both packages are in one project. Can i now start a maven build to get a foo.jar and a bar.jar?

Thanks 4 the help.

EDIT: Okay, I think you need some more informations why I am asking that. I have a project which is divided in two parts (a library and a daemon) and both parts must be runnable independent from each other, so I want to use a own jar for each part. I was thinking about a "parent project" that includes the two parts as modules like some of you suggested (is that a nicer way?). The next point is that I have only one git repo in that I want to commit the whole project. Is it possiple to place the modules projects content in the parent project like src/lib/java or how I have to do that?

F481
  • 990
  • 8
  • 22

3 Answers3

2

Create a project for every package to adhere to Maven conventions. A quick question, why do you need to do that?

Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
  • You're definitely on the good track, a Maven multi-module project can be placed on a single git repository. This SO question should help http://stackoverflow.com/questions/7841440/eclipse-maven-git-multi-module-projects-unhappiness – Toni Toni Chopper Nov 30 '12 at 11:29
  • so this is the right structure? http://img706.imageshack.us/img706/923/screenwfi.png – F481 Nov 30 '12 at 13:05
2

The maven jar plugin can be configured to include and exclude classes. So it would be easy to just package all classes from one package:

     <includes>
        <include>com/example/foo/*</include>
      </includes>
      <excludes>
        <exclude>com/example/foo/subfoo1/**</exclude>
        <exclude>com/example/foo/subfoo2/**</exclude>
        <!-- to exclude all packages that start with com.example.foo -->
      </excludes>

But you'd have to write a pom file for each and every single package... You could create on additional multi-module pom, that runs all those modules and you could even auto-generate the pom files with a small java programm, although it's quite challenging to auto-detect all packages that would go into the jars in advance.


It may be easier to use the maven AntRun plugin and define a <target> that calls the jar task for each package.


The multi-module structure could be like this:

./
 pom.xml
 project/
        src/...
        target/...
        pom1.xml
        pom2.xml
        pom3.xml

it was my understanding, that all java packages reside in the same project. If the packages are in different projects, then you wouldn't have any trouble packaging them in different jars - that would be the default behavior.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • so this could be the structure for a multi-module solution? http://img706.imageshack.us/img706/923/screenwfi.png – F481 Nov 30 '12 at 13:58
0

In Maven the conventions is to build a jar for a module which means in other words to build a jar from the contents of src/main/java. If you need to build a jar from every packages that sounds a little bit weird to me. You can do things like this but this is not recommended cause it will result in trouble later. This is a kind of fight you start with Maven which you will loose in the end.

You need to configure the maven-jar-plugin:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <excludes>
            <exclude>...</exclude>
          </excludes>
          <includes>
            <include>com/mypackage/foo/**</include>
          </includes>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
F481
  • 990
  • 8
  • 22
khmarbaise
  • 92,914
  • 28
  • 189
  • 235