Skip to content
Snippets Groups Projects
Commit 4abf0fcd authored by Joel Cavat's avatar Joel Cavat
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
# Maven
Maven est un gestionnaire de projets et d'automatisation de production pour l'écosystème Java.
Le fichier `pom.xml` décrit le projet avec ses dépendances.
### Modification du fichier pom.xml
Adaptez le fichier `pom.xml` selon votre projet :
- Verifiez la version de java
- Changez le nom du projet/package dans `groupId` et `artifactId`
- Modifiez la configuration du plugin `exec-maven-plugin` pour pointer sur votre classe principale contenant votre `main`
### Exécutez votre projet
- compilation et téléchargement des dépendances. Produit un `jar` dans le dossier `target`.
```
mvn package
```
- exécutez à l'aide de java et du classpath:
```
java -cp target/my-app-0.1.jar ch.hepia.my_app.App
```
- ou, à l'aide du plugin `exec-maven-plugin`:
```
mvn exec:java
```
### Exécutez les tests uniquement
```
mvn test
```
### Notes
Le plugin `maven-assembly-plugin` est configuré et permet d'embarquer les dépendances dans un seul `jar`.
### Nouvelles dépendances
Pour ajouter une nouvelle dépendance, vous pouvez l'ajouter dans la section `<dependencies>`. Le site [mvnrepository](https://mvnrepository.com) est une référence pour la mise à disposition de librairies de l'écosystème de la JVM (Java, Scala, Kotlin...)
Vous pouvez par exemple ajouter la librairie JavaFX pour réaliser des interfaces graphiques en ajoutant le code ci-dessous dans votre fichier `pom.xml`:
```
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>14</version>
<type>pom</type>
</dependency>
```
\ No newline at end of file
pom.xml 0 → 100644
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.hepia.my_app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>my-app</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Point here to your main class -->
<mainClass>ch.hepia.my_app.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<excludes>
<exclude>some test to exclude here</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package ch.hepia.my_app;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
Scene scene = new Scene(new StackPane(l), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
package ch.hepia.my_app;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* Unit test for simple App.
*/
class AppTest {
@Test
void dummyTest() {
assertTrue(true);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment