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?

有帮助吗?

解决方案

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.

其他提示

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:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top