2

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.

gfytd
  • 1,747
  • 2
  • 24
  • 47

1 Answers1

1

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

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • for the 'sledge hammer', all other packages are also removed from jar, this is not what I want. – gfytd Feb 01 '19 at 08:25
  • ...you con combine multiple ``s ..with wildcards. – xerx593 Feb 01 '19 at 08:27
  • :) that doesn't sound like a good way, since I have quite a few packages – gfytd Feb 01 '19 at 08:34
  • so when there is no "easy way" (with wildcards), then there is no "easy way" at all! :), but experiment a little, research the doc of these "includes/excludes", and maybe you find a suitable solution. – xerx593 Feb 01 '19 at 08:35
  • a big fat 'NO WAY' is good enough, thank you. I can actually put that Dummy class in a separate package then use 'exclude'. I'm just curious to know if there's any magic trick in maven, good to know there's no easy way to go. – gfytd Feb 01 '19 at 08:48