M Sach
M Sach

Reputation: 34424

How to build specific folder in maven?

I have a folder myApp1 and pom.xml is lying inside myApp1. Here is the hirerchy of myApp1

myAppl1
  |--src
       |--main
             |--java 
               (Java Source Code)
             |--resources
               some xml files and doc files 
  |--pom.xml

Now if i do mvn package it build all the folders and makes jar files(basically it puts also all the files under resources folder in jar file).

What i want is to build the files under java folder only which does not have any dependency on resources folder(java files dependencies are mentioned in POM file which are jars in repository).

I am not sure how we can build the specific folder in maven. I tried this mvn install -pl src\main\java -am but says Could not find the selected project in the reactor?

EDIT This is how i had put it in pom.xml before

<pluginManagement>
   <plugins>
<plugin> 
        <artifactId>maven-jar-plugin</artifactId> 
        <version>${maven-jar-plugin.version}</version> 
        <configuration> 
            <verbose>true</verbose> 
            <excludes> 
                <exclude>resources/*</exclude> 
                <exclude>webapp/*</exclude>                     
            </excludes> 
        </configuration> 
</plugin> 
</plugins>
 </pluginManagement>

But i get [ERROR] Malformed POM C:\myProject\pom.xml: Unrecognised tag: 'pluginMana gement' (position: START_TAG seen ...\r\n\r\n ... @55:21) @ C:\myProject\pom.xml, line 55, column 21 -> [Help 2]

Upvotes: 2

Views: 10114

Answers (4)

Hannes Kogler
Hannes Kogler

Reputation: 461

for files that shouldn't be copied into the artifact file you can use the projects src/main/config directory, which fullfills also the maven naming conventions.

in my special case I use this directory for storing binary sources like .exe files aso. that get zipped by the assembly plugin and are deployed to my repository as zip and bin which works well and seems like a clean workflow.

Upvotes: 1

Michał Politowski
Michał Politowski

Reputation: 4385

This is going to be a "don't do this" kind of answer.

Maven expects the resources to be files that have to end up in your artifact, that's what resources are for. If you have other files they really should be put somewhere else.

Having said that, it is possible to confuse the heck of everyone expecting the conventional behaviour and to tell maven that files in src/main/resources are in fact not resources.

To do that say in your POM build section

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <excludes>
      <exclude>**/*</exclude>
    </excludes>
  </resource>
</resources>    

Resource copying is handled by the maven resources plugin, so its documentation is worth a look for resources related trouble.

Upvotes: 2

Jugal Shah
Jugal Shah

Reputation: 3695

You have to use maven-jar-plugin and configure it to exclude the files or directories which you do not want to include it in your JAR. Also, check out its usage on Maven site. Following is a sample configuration for maven-jar-plugin:

    <build>
    ...
    <plugins>
    ...
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin.version}</version>
            <configuration>
                <verbose>true</verbose>
                <excludes>
                    <exclude>properties</exclude>
                    <exclude>properties/*</exclude>
                    <exclude>spring</exclude>
                    <exclude>spring/*</exclude>
                    <exclude>logback.xml</exclude>
                </excludes>
            </configuration>
        </plugin>
     ...
     </plugins>
     ...
     </build>

Upvotes: 3

Yanflea
Yanflea

Reputation: 3934

You must put the maven-jar-plugin declaration in the < build > section rather than in < pluginManagement >.

Upvotes: 2

Related Questions