Mavenメモ

## Mavenバッチ実行例

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/176603/78c749b5-baa2-43c5-847e-35705ac6cf60.png)


```BatchProgram.java
package com.example.batch_demo;

public class BatchProgram {

	public static void main(String[] args) {
        System.out.println("Batch program started...");
        if (args.length > 0) {
            System.out.println("Arguments received:");
            for (String arg : args) {
                System.out.println(" - " + arg);
            }
        } else {
            System.out.println("No arguments provided.");
        }
        System.out.println("Batch program completed.");
	}

}
```

・C:\pleiades\2023-12\workspace\batch-demo
```run.bat
@echo off
echo Starting the batch program...
java -jar "%~dp0..\target\batch-demo-1.0-SNAPSHOT.jar" %*
echo Batch program finished.
pause
```

・C:\pleiades\2023-12\workspace\batch-demo
```run.bat
@echo off
echo Starting the batch program...
java -cp target/batch-demo-1.0-SNAPSHOT.jar com.example.batch_demo.BatchProgram arg1 arg2
echo Batch program finished.
pause
```

(実行結果)
```
C:\pleiades\2023-12\workspace\batch-demo>batch\run.bat arg1 arg2
Starting the batch program...
Batch program started...
Arguments received:
 - arg1
 - arg2
Batch program completed.
Batch program finished.
続行するには何かキーを押してください . . .

C:\pleiades\2023-12\workspace\batch-demo>java -cp target/batch-demo-1.0-SNAPSHOT.jar com.example.batch_demo.BatchProgram arg1 arg2
Batch program started...
Arguments received:
 - arg1
 - arg2
Batch program completed.

C:\pleiades\2023-12\workspace\batch-demo>java -jar target\batch-demo-1.0-SNAPSHOT.jar arg1 arg2 arg3
Batch program started...
Arguments received:
 - arg1
 - arg2
 - arg3
Batch program completed.

C:\pleiades\2023-12\workspace\batch-demo>run.bat
Starting the batch program...
Batch program started...
Arguments received:
 - arg1
 - arg2
Batch program completed.
Batch program finished.
続行するには何かキーを押してください . . .

```

```pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>batch-demo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>batch-demo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>17</maven.compiler.release>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.11.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Optionally: parameterized tests support -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-params</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.4.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.3.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.13.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.3.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.12.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.6.1</version>
        </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
					<id>shade</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.batch_demo.BatchProgram</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      </plugins>
  </build>
</project>

```