0

I'm trying to build a non-executable jar file that only includes certain packages from my project to use as a library for common code. I'm planning on deploying that to Nexus and including it in other projects.

I can make it manually in eclipse by right clicking on the project, selecting Export, selecting JAR file (NOT Runnable Jar File) then checking the packages that I want to include. I'm trying to mimic this behavior in Maven. I have been able to replicate the non-runnable part in maven by not including a main in the manifest, but still have to find out how to include only certain packages.

This project actually builds 2 jar files already (from 2 different main's in the program - a test GUI and the regular application). I'm having trouble finding information on how to do the non-runnable jar and selecting certain packages only.

Here is the existing build portion of my pom file is below. Build-third is the non-runnable jar:

    <plugins>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>build-first</id>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>com.joy.fb20.da.exec.jDaExecutive</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>DaExecutive</finalName>
            </configuration>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
        <execution>
            <id>build-second</id>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>com.joy.fb20.gui.jExampleAppGui</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>ExampleAppGui</finalName>
            </configuration>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
<execution>
            <id>build-third</id>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>ddslib</finalName>
            </configuration>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </plugin>
      </plugins>
    </build>
Tacitus86
  • 1,314
  • 2
  • 14
  • 36
  • How about separating that part as own artifact project and adding it as dependency to your original project? As well the others that need it? – pirho Oct 31 '17 at 16:45
  • I wanted to do that initially but the maintainer of the code refuses to split the source so my hands are tied in doing it in this method. – Tacitus86 Oct 31 '17 at 16:46

2 Answers2

2

Tried this myself and it is possible.

  • Create a new maven profile
  • Add pluginManagement section to that new profile for maven-jar-plugin and if needed for maven-resources-plugin also
  • Use this pluginManagement to configure inclusions/exclusions of sources/resources and override artifacts final name here to have different name for that separate jar

Example

<profiles>
   <profile>
      <id>subset</id>
      <build>
         <pluginManagement>
            <plugins>
               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <version>3.0.2</version>
                  <configuration>
                     <finalName>subset</finalName>
                     <includes>
                        <include>org/example/stuff/**</include>
                     </includes>
                  </configuration>
               </plugin>
            </plugins>
         </pluginManagement>
      </build>
   </profile>
</profiles>

Using this profile maven creates subset.jar that has everything under org/example/stuff/**

See more about excluding: How to exclude a set of packages from maven build jar?

Of course it might be possible to do all this also without pluginManagement but in my opinion pluginManagement provides clean & flexible way to configure this kind of stuff so i recommend that.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • I had to separate the original builds into a profile and the new one into its own profile and this worked. – Tacitus86 Oct 31 '17 at 19:12
0

You should be able to do that by using a custom assembly descriptor and specifying your fileset and / or file includes / excludes. See https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_fileSet

gpeche
  • 21,974
  • 5
  • 38
  • 51
  • I tried doing this, by using exclude everything -> * and then including the packages I want and it seems like it just totally ignores these commands. – Tacitus86 Oct 31 '17 at 18:10