1

I have a Maven module with two different database profiles.

    <profile>
        <id>db-localhost-oracle</id>
        <dependencies>
            <dependency>
                <groupId>ojdbc6</groupId>
                <artifactId>ojdbc6</artifactId>
            </dependency>
        </dependencies>
        <properties>
            <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>
            <db.dialect>no.jbv.sergej.util.FixedOracle10gDialect</db.dialect>
            <db.url>jdbc:oracle:thin:@//localhost:1521/xe</db.url>
            <db.hbm2ddl>update</db.hbm2ddl>
        </properties>
    </profile>

    <profile>
        <id>db-localhost-mysql</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
        </dependencies>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.dialect>org.hibernate.dialect.MySQL5Dialect</db.dialect>
            <db.url>jdbc:mysql://localhost/${mysql.schema}</db.url>
            <db.hbm2ddl>update</db.hbm2ddl>
        </properties>
    </profile>

When is run maven install with "db-localhost-mysql" it includes the "mysql-connector-java" jar file in lib directory. Now I do clean install with "db-localhost-oracle" and it includes the both "mysql-connector-java" and "ojdbc6" jars in the lib directory.

How can I make it like, if I build with one profile maven automatically remove the jars for other profile?

2 Answers2

2

Your problem does not match what should happen in practice. Your profile definition sounds about right to me:

mvn clean install will enable the db-localhost-mysql (as it is marked as to be activated by default) and it will add mysql-connector-java. The same will happen if you run mvn clean install -Pdb-localhost-mysql

mvn clean install -Pdb-localhost-oracle will add the ojdbc6 driver. The mysql profile will not be enabled (as it is triggered only if no profile is explicitly active).

That does not mean your current dependency hierarchy hasn't already one of those jars. It might come as a transitive dependency. To isolate this case and know which project needs to be fixed run mvn dependency:tree -Pdb-localhost-oracle to look at your dependencies hierarchy when the mysql profile is not enabled.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • I have found that if I remove activeByDefault from db-localhost-mysql than it works as expected. i.e mysql-connector-java jar is removed when i build with db-localhost-oracle. – Mansoor Sajjad Mar 04 '14 at 12:00
  • Is it like that, if any profile is marked as activeByDefault, it remains active even if we select any other profile? – Mansoor Sajjad Mar 04 '14 at 12:01
  • No. It's only active if **no** profile is enabled _This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config._ See [the documentation](http://maven.apache.org/guides/introduction/introduction-to-profiles.html) – Stephane Nicoll Mar 04 '14 at 12:46
  • Yes thats true, the profile is activated as expected. There isn't any problem with that, but the problem is if I use activeByDefault with the profile, the dependency jars for that profile will always be downloaded even if I use some other profile to build the project. Why is it so and how to get rid of this thing? I don't want extra jars. – Mansoor Sajjad Mar 04 '14 at 13:08
  • I just copy/pasted your pom snippet in one of my project and the dependency:list is different. Again, what you describe is not how profiles work with Maven. Please read my original answer. My best guess is that this dependency is already coming from another dependency in your project and `mvn dependency:tree` (without that profile) will help you track where it is coming from. Try `mvn dependency:tree -Pdb-localhost-oracle` to check who is pulling the mysql driver. – Stephane Nicoll Mar 04 '14 at 14:58
0

I assume you download your downloaded dependencies using maven-dependency-plugin somewhere outside your target dir (${basedir}/lib).

If that is the case, you would need to include your lib dir inside your clean definition (see http://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html):

<build>
  [...]
  <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <configuration>
      <filesets>
        <fileset>
          <directory>lib</directory>
        </fileset>
      </filesets>
    </configuration>
  </plugin>
  [...]
</build>

However: Please consider doing it differently:

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
blackbuild
  • 5,026
  • 1
  • 23
  • 35