Vediamo alcuni utili consigli per scrivere al meglio il file pom.xml del nostro progetto Maven.
Aggiornamenti
11/10/2016 Installare dei plugin Maven per lavorare in offline
27/09/2016 Creazione articolo
La seguente istruzione indica il formato di uscita che deve avere il nostro progetto:
<packaging>jar</packaging>
In questo caso quando faremo
mvn package
in uscita, nella cartella target, avremo il nostro file jar.
Il seguente plugin serve a mettere in un unico jar i sorgenti delle dipendenze:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>groupid:artifactid:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
Il seguente plugin serve a mettere in un unico jar i sorgenti delle dipendenze:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>groupid:artifactid:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
Il seguente plugin mette nella cartella classe la cartella lib con le dipendenze:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
<excludeArtifactIds>junit</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
<excludeArtifactIds>junit</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
Il seguente plugin serve ad avere in offline le dipendenze:
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase>
<goals> <goal>go-offline</goal> </goals> </execution> </executions> </plugin>
<executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase>
<goals> <goal>go-offline</goal> </goals> </execution> </executions> </plugin>
il seguente plugin serve per la creazione del jar finale.
Il parametro addClasspath serve per inserire nel classpath il puntamento alla cartella lib inserita nel jar.
Il parametro manifestEntries indica cosa includere nel jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Il seguente plugin serve ad includere nel jar i sorgenti:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Affrontiamo ora un problema molto comune.
Come si fa ad installare un plugin Maven?
Di solito è una procedura alquanto automatica, perché quando facciamo la build del nostro progetto in automatico le dipendenze Maven vengnon scaricate.
Ma se noi le vogliamo rendere disponibili in modalità offline come si fa?
Questa è l'istruzione da usare:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]
Ad esempio:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.apache.maven:maven-model-builder:3.0
Buon lavoro!