aguibert / openliberty-cheat-sheet

Concise tips and examples for using Open Liberty
https://aguibert.github.io/openliberty-cheat-sheet/
Apache License 2.0
19 stars 9 forks source link

Maven: add shared resources to liberty:dev maven goal #4

Open bmarwell opened 4 years ago

bmarwell commented 4 years ago

Hi Andy,

as you probably have noticed on twitter, there currently seems to be no elegant way to supply shared resources to the liberty:dev maven goal. So I thought you might want to add how to do this?

Expected outcome

Use a library from the ${shared.resource.dir}.

Relevant part from src/main/liberty/config/server.xml:

  <jdbcDriver id="h2" javax.sql.DataSource="org.h2.jdbcx.JdbcDataSource">
    <library description="H2 Database" name="h2">
      <fileset dir="${shared.resource.dir}/h2" includes="h2*.jar" />
    </library>
  </jdbcDriver>

  <dataSource jdbcDriverRef="h2" jndiName="jdbc/oracledb" type="javax.sql.DataSource">
    <connectionManager maxPoolSize="5" minPoolSize="2" />
    <properties URL="jdbc:h2:mem:oracledb;MODE=oracle;LOG=1;USER=sa;password=" password="" user="sa" />
  </dataSource>

Hassle with the liberty-maven-plugin

liberty:dev currently does not support copying artefacts to the ${shared.resource.dir} itself. That's okay, the liberty-maven-plugin should probably only manage liberty.

But there is a caveat:

Suppose you execute mvn dependency:copy@goal liberty:dev.

Working solution

So I came up with this configuration in my pom.xml which redefines wlp.usr.dir/WLP_USR_DIR. I think this could possible be added to the cheat sheet.

  1. Define two new properties:
      <wlp.usr.dir>${project.build.directory}</wlp.user.dir>
      <wlp.shared.resource.dir>${wlp.usr.dir}/shared/resources</wlp.shared.resource.dir>
  1. Copy to that new directory:
      <plugin>
        <groupId>io.openliberty.tools</groupId>
        <artifactId>liberty-maven-plugin</artifactId>
        <configuration>
          <userDirectory>${wlp.usr.dir}</userDirectory>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
          <execution>
            <id>copy-shared-resources</id>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.h2database</groupId>
                  <artifactId>h2</artifactId>
                  <version>${dependency.h2.version}</version>
                  <outputDirectory>${wlp.shared.resource.dir]/h2</outputDirectory>
                  <destFileName>h2-${dependency.h2.version}.jar</destFileName>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>

Maybe this solution can be added to the examples? I figured out having wlp.usr.dir (or env WLP_USR_DIR in a subdirectory of a liberty installation is (almost) never a good idea. I have a few (open)liberty versions on my machines, and I use the same WLP_USR_DIR for all of them.

scottkurz commented 4 years ago

I would have solved the problem here by doing:

mvn liberty:create dependency:copy@goal liberty:dev.

Then dev mode won't install over target/liberty/wlp/ since it's already there.

It can also be nice to use a separate WLP_USER_DIR but it's not always clean, maybe you don't have write access. It's nice to use the maven build dir too.

bmarwell commented 4 years ago

Thanks, didn't even think of liberty:create as it was already included in liberty:dev. Good catch!

I agree, I really like to have a separate WLP_USER_DIR, but it really should stay inside ${project.build.directory}. Actually I had a typo and meant ${project.build.directory}/wlp-usr/.