You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Peter Horlock <pe...@googlemail.com> on 2008/06/09 14:43:20 UTC

Excluding packages and classes from jar

Hi,

I would like to define a profile to exclude certain packages and classes
from the jar builded -
we have an internal common package containing shared functionality, but for
a specific project for a specifc client I have to exclude certain classes
and packages
(because of security and licencing problems...)

I added something like this to my pom:
    <profile>
        <id>myflag</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <excludes>

<exclude>**/com/mycompany/mysubpackage/packageA/packageB/*</exclude>

<exclude>**/com/mycompany/mysubpackage/packageC/MyClass.java</exclude>
                                </excludes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

and called it like that:mvn package -P myflag

However, it didn't work! :-(
Any ideas, what wrong?

Thanks in advance,

Peter

RE: Excluding packages and classes from jar

Posted by Jörg Schaible <Jo...@Elsag-Solutions.com>.
Hi Peter,

what is the complete configuration for the plugin? Have a look at "mvn help:effective-pom". Remember, that the configuration is merged from the (inherited) pluginManagement section. Note, that in other plugin configuration with includes/excludes the exclude is ignored, if an include is defined (although this is not stated in the maven-jar-plugin's docs). This might also apply here.

- Jörg

Peter Horlock wrote:
> Hi Rémy, thanks for your reply. I also tried mvn package -Pmyflag,
> but this did't work out either - btw -p SPACE FLAG is what is said on
> the maven docu
> site:
> http://maven.apache.org/guides/introduction/introduction-to-pr
> ofiles.html : 
> 
> mvn groupId:artifactId:goal -P profile-1,profile-2
> 
> However, the new Maven - The definitive Guide - Pdf book says
> -PMYFLAG. 
> 
> -------
> Next, as you suggested, I tried it directly without a profile, and it
> neither worked :-( I am not even sure if what I am trying is supposed
> to be working - I am trying to exclude certain classes or package -
> not simple 
> exluding a certain src folder, like "include "src/java" but exclude
> "src/java2" or so - I am trying to do in on a package or class basis -
> 
>                     <plugin>
>                         <groupId>org.apache.maven.plugins</groupId>
>                         <artifactId>maven-jar-plugin</artifactId>
>                         <executions>
>                             <execution>
>                                 <phase>package</phase>
>                                 <goals>
>                                     <goal>jar</goal>
>                                 </goals>
>                                 <configuration>
>                                     <excludes>
> 
> <exclude>**/com/mycompany/mypackage/mysubpackage/MyClassA.java
> </exclude> 
> 
> <exclude>**/com/mycompany/mypackage/mysubpackage/packageA/*</exclude>
> 
> <exclude>**/com/mycompany/mypackage/mysubpackage/packageB/*</exclude>
>                                     </excludes>
>                                 </configuration>
>                             </execution>
>                         </executions>
>                     </plugin>
> 
> I suppose the exclude paths are wrong - but how should I define them?
> src/java/com...? 
> 
> Thanks in advance,
> 
> Peter


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


Re: Excluding packages and classes from jar

Posted by Peter Horlock <pe...@googlemail.com>.
>Where did you get that configuration parameters from? The install-plugin's
documentation [1] doesn't mention these.

Well, to be honest, most maven plugin documentations are --hmmmm, rather
short, to say the least.
So often I just have a look into the source by using the report source html
pages - there I found a private attribute artifactId, so I just tried it.
This time, it did not work, but often this helps in finding hidding
configuration options...
-----
Last but not least - thanks for your help - now everything works as it
should! THANKS! :-)

Peter

Re: Excluding packages and classes from jar

Posted by Tim Kettler <ti...@udo.edu>.
Peter Horlock schrieb:
> thanks a lot, this helped.
> 
> One more, however:
> Now it creates the jar as it should, but I would like to have a different
> name for it when installating it into the local / remote repo.
>
> Under target, it creates:
> myproject-mysubversion-1.1.jar
> 
> but when installing or deploying, it installs /deploys it as:
> myproject-1.1.jar (just like the jar that contains all classes).

All artifacts in a repository have to follow a common naming schema so 
that maven can locate them. The schema is:

   artifactid-version[-classifier].packaging

This is fixed, you can't put arbitrary named jars in there.

> I tried:
>             <plugin>
>                         <artifactId>maven-install-plugin</artifactId>
>                         <configuration>
>                              <artifactId>myproject-mysubversion</artifactId>
>                         </configuration>
>          </plugin>
> 
> as well as:
>             <plugin>
>                         <artifactId>maven-install-plugin</artifactId>
>                         <configuration>
> <file>yproject-mysubversion-${version}.${packaging}</file>
>                         </configuration>
>          </plugin>

Where did you get that configuration parameters from? The 
install-plugin's documentation [1] doesn't mention these.

> but both just installed the file as myproject-1.1.jar into my repository.
> This could lead to really bad confusions... :-(

That's expected, the install-plugin doesn't know these parameters and 
just ignores them.

> Any ideas?

I would configure it like this in my pom:

   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <executions>
       <execution>
         <id>package-stripped-jar</id>
         <phase>package</phase>
         <goals>
           <goal>jar</goal>
         </goals>
         <configuration>
           <classifier>stripped</classifier>
           <excludes>
             <exclude>pkg1/Class1.class</exclude>
             <exclude>pkg2/**</exclude>
           </excludes>
         </configuration>
       </execution>
     </executions>
   </plugin>

This configures a second execution of the jar plugin in addition to the 
default execution because of the jar-packaging. This second execution 
creates the stripped down jar with the given classifier and attaches it 
to the build.

The install (and deploy) plugin will automatically pick up the main 
artifact (created by the default jar execution) and the second one with 
the clsssifier and installs/deploys them to the repository.

when you need to reference the stripped plugin in another project the 
dependency would look like this:

   <dependency>
     <groupId>mygroup.id</groupId>
     <artifactId>my-artifact</artifactId>
     <version>3.14159</version>
     <classifier>stripped</classifier>
   </dependency>

> Thanks guys,
> 
> Peter Horlock
> 

-Tim

[1] http://maven.apache.org/plugins/maven-install-plugin/install-mojo.html

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


Re: Excluding packages and classes from jar

Posted by Peter Horlock <pe...@googlemail.com>.
thanks a lot, this helped.

One more, however:
Now it creates the jar as it should, but I would like to have a different
name for it when installating it into the local / remote repo.

Under target, it creates:
myproject-mysubversion-1.1.jar

but when installing or deploying, it installs /deploys it as:
myproject-1.1.jar (just like the jar that contains all classes).
I tried:
            <plugin>
                        <artifactId>maven-install-plugin</artifactId>
                        <configuration>
                             <artifactId>myproject-mysubversion</artifactId>
                        </configuration>
         </plugin>

as well as:
            <plugin>
                        <artifactId>maven-install-plugin</artifactId>
                        <configuration>
<file>yproject-mysubversion-${version}.${packaging}</file>
                        </configuration>
         </plugin>

but both just installed the file as myproject-1.1.jar into my repository.
This could lead to really bad confusions... :-(

Any ideas?

Thanks guys,

Peter Horlock

Re: Excluding packages and classes from jar

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

Peter Horlock schrieb:
> Hi Rémy, thanks for your reply. I also tried mvn package -Pmyflag, but this
> did't work out either - btw -p SPACE FLAG is what is said on the maven docu
> site:
> http://maven.apache.org/guides/introduction/introduction-to-profiles.html :
> 
> mvn groupId:artifactId:goal -P profile-1,profile-2
> 
> However, the new Maven - The definitive Guide - Pdf book says -PMYFLAG.
> 
> -------
> Next, as you suggested, I tried it directly without a profile, and it
> neither worked :-(
> I am not even sure if what I am trying is supposed to be working - I
> am trying to exclude certain classes or package - not simple
> exluding a certain src folder, like "include "src/java" but exclude
> "src/java2" or so - I am trying to do in on a package or class basis -
> 
>                     <plugin>
>                         <groupId>org.apache.maven.plugins</groupId>
>                         <artifactId>maven-jar-plugin</artifactId>
>                         <executions>
>                             <execution>
>                                 <phase>package</phase>
>                                 <goals>
>                                     <goal>jar</goal>
>                                 </goals>
>                                 <configuration>
>                                     <excludes>
> 
> <exclude>**/com/mycompany/mypackage/mysubpackage/MyClassA.java</exclude>
> 
> <exclude>**/com/mycompany/mypackage/mysubpackage/packageA/*</exclude>
> 
> <exclude>**/com/mycompany/mypackage/mysubpackage/packageB/*</exclude>
>                                     </excludes>
>                                 </configuration>
>                             </execution>
>                         </executions>
>                     </plugin>
> 
> I suppose the exclude paths are wrong - but how should I define them?
> src/java/com...?

The jar-plugin works on the compiled output in target/classes, so 
ecluding *.java files can't work. I just tried with a simple test project:

   .
   |-- pom.xml
   `-- src
       `-- main
           `-- java
               |-- pkg1
               |   |-- Class1.java
               |   `-- Class2.java
               `-- pkg2
                   |-- Class1.java
                   `-- Class2.java


and this plugin configuration in the pom:

   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <configuration>
       <excludes>
         <exclude>pkg1/Class1.class</exclude>
         <exclude>pkg2/**</exclude>
       </excludes>
     </configuration>
   </plugin>

After 'mvn package' the resulting jar contained just pkg1/Class2.class, 
just as expected.

> Thanks in advance,
> 
> Peter
> 

Hope this helps
-Tim

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


Re: Excluding packages and classes from jar

Posted by Peter Horlock <pe...@googlemail.com>.
Hi Rémy, thanks for your reply. I also tried mvn package -Pmyflag, but this
did't work out either - btw -p SPACE FLAG is what is said on the maven docu
site:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html :

mvn groupId:artifactId:goal -P profile-1,profile-2

However, the new Maven - The definitive Guide - Pdf book says -PMYFLAG.

-------
Next, as you suggested, I tried it directly without a profile, and it
neither worked :-(
I am not even sure if what I am trying is supposed to be working - I
am trying to exclude certain classes or package - not simple
exluding a certain src folder, like "include "src/java" but exclude
"src/java2" or so - I am trying to do in on a package or class basis -

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <excludes>

<exclude>**/com/mycompany/mypackage/mysubpackage/MyClassA.java</exclude>

<exclude>**/com/mycompany/mypackage/mysubpackage/packageA/*</exclude>

<exclude>**/com/mycompany/mypackage/mysubpackage/packageB/*</exclude>
                                    </excludes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

I suppose the exclude paths are wrong - but how should I define them?
src/java/com...?

Thanks in advance,

Peter

Re: Excluding packages and classes from jar

Posted by Rémy Sanlaville <re...@gmail.com>.
Hi,

and called it like that:mvn package -P myflag
>

Not sure but you have an extra space between -P and myflag
try mvn package -Pmyflag

If it is not working, first try without profile in order to be sure that the
exclusion configuration is working.
HTH,

Rémy