gfytd
gfytd

Reputation: 1809

Maven, exclude all classes but one under a package

This may sound silly, but I really want to know if I can do that (by adding some 'magic' configuration in pom.xml).

Let's say, I have a project which has a bunch of packages, one of them, say 'com.foo.bar', has quite a few .java files, including one named 'Dummy.java'.

Now, when generating the jar file, I want to exclude all classes under com.foo.bar, except the 'Dummy'.

I tried regular expression in a section, but no luck.

Is there an easy way to go? Many thanks.

Upvotes: 2

Views: 1222

Answers (1)

xerx593
xerx593

Reputation: 13261

With a "sledge hammer" (assuming Dummy.class not .java):

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <includes>
        <include>com/foo/bar/Dummy.class</include>
    </includes>
    <!-- alternatively(!) excludes/exclude* -->
  </configuration>
</plugin>

... or with a "Swiss army knife": maven-assembly-plugin ... ^^!"%/"))

...

To use the Assembly Plugin in Maven, you simply need to:

  1. choose or write the assembly descriptor to use,
  2. configure the Assembly Plugin in your project's pom.xml,
  3. and run "mvn assembly:single" on your project. ...

with an assembly descriptor like:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 
   http://maven.apache.org/xsd/assembly-2.0.0.xsd">
   <id>dummy-only</id>
   <formats>
     <format>jar</format>
   </formats>
   <includeBaseDirectory>false</includeBaseDirectory>
   <fileSets>
     <fileSet>
     <outputDirectory>/</outputDirectory>
     <directory>${project.build.outputDirectory}</directory>
     <includes>
       <include>com/foo/bar/Dummy.class</exclude>
     </includes>
   </fileSet>
  </fileSets>
</assembly>

see also: I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error

Upvotes: 1

Related Questions