maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jörg Schaible <joerg.schai...@gmx.de>
Subject Re: same proyects running under java 1.4 and java 1.5
Date Wed, 13 Apr 2011 16:31:33 GMT
Hi,

Fernando Wermus wrote:

> Hi all,
>     We have a proyect which we need to compile under java 1.4 for some
> companies and 1.6 for other companies. The problem is that in java 1.6 we
> have a class that implements an interface named ResulSet. Result changes
> its methods from 1.4 to 1.6 because jdbc changes. Then we need to have an
> implementation of Resulset for java 1.4 and another impl for java 1.6.
> What we need is to add to our currents profiles a way to include or
> exclude some class in the proyect with the same package and class name.
> 
> Is any solution from maven for this problem?
> 
> thanks in advance and I hope you have understood our problem.

This is how we did it:

1/ Use two independent version lines i.e. do it like Apache commons dbcp 
where version 1.3.x is for JDBC 3 (Java 5 or lower) and version 1.4.x is for 
JDBC 4 (Java 6).

2/ Use comment markers in your Java code, see following snippet:

============= %< =================
/*JDBC4**/
import java.sql.NClob;
import java.sql.RowId;
import java.sql.SQLXML;
//*JDBC4*/
============= %< =================

All types and methods that are only available for JDBC 4 are surrounded with 
those two markers i.e. the code is compilable from your IDE with Java 6 as 
usual.

3/ Prepare the POM to modify the source using a profile based on the target 
JDK.

The trick is to use the original source that compiles against JDBC 4 and 
filter it (resp. generate new sources) with a regular expression that uses 
those two markers to create Java block comments and let the compiler use the 
new sources then.

=============== %< ===========
...
  <groupId>your.company</groupId>
  <artifactId>your.artifact</artifactId>
  <version>${version.your.artifact}</version>
...
  <build>
    <sourceDirectory>${local.source.directory}</sourceDirectory>
  </build>
...
  <profiles>
    <profile>
      <id>jdk15</id>
      <activation>
        <jdk>1.5</jdk><!-- JDBC 3 only -->
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>your.company</groupId>
            <artifactId>regexp-plugin</artifactId>
            <executions>
              <execution>
                <id>filter-jdbc4</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>perform</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <regExpFilter>
                <sourceDir>${basedir}/src/main/java</sourceDir>
                <targetDir>${basedir}/target/generated-
sources/java</targetDir>
	   <regExps>
                  <regExp>
                    <search>/\*JDBC4\*\*/</search>
                    <replace>/*JDBC4</replace>
                  </regExp>
                  <regExp>
                    <search>//\*JDBC4\*/</search>
                    <replace>JDBC4*/</replace>
                  </regExp>
                </regExps>
              </regExpFilter>
            </configuration>
          </plugin>
        </plugins>
      </build>
      <properties>
        <local.source.directory>target/generated-
sources/java</local.source.directory>
      </properties>
    </profile>
  </profiles>
...
  <properties>
    <local.source.directory>src/main/java</local.source.directory>
  </properties>
=============== %< ===========

The property "version.your.artifact" is defined in the parent and the 
artifact is also declared there in a depMgmt section:

=============== %< ===========
...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>your.company</groupId>
        <artifactId>your.artifact</artifactId>
        <version>${version.your.artifact}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
...
  <profiles>
    <profile>
      <id>jdk15</id>
      <activation>
        <jdk>1.5</jdk>
      </activation>
      <properties>
        <version.your.artifact>1.6.0-SNAPSHOT</version.your.artifact>
      </properties>
    </profile>
  </profiles>
...
  <properties>
    <version.your.artifact>1.5.0-SNAPSHOT</version.your.artifact>
  </properties>
=============== %< ===========

Now, the plugin we use for the regexp fitlering is not publicly available, 
but if you look at Apache dbcp, it is done there with an Ant task and you 
should be able to to similar with the antrun plugin. I've also seen once 
that plexus-utils have capabilities for regexp filtering, but I don't know 
if there's any syntax to activate this for standard Maven filtering. OTOH it 
is really easy to write such a simply plugin yourself ;-)

Note, we used the current JDK for profile activation i.e. it depends on the 
Java version we use to run Maven itself. This might not be appropriate, you 
will have to define different activations then.

Hope this gives you some ideas,
Jörg



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


Mime
View raw message