Install and configure the Exec Maven Plugin now to run your Java main class during a build. In your pom.xml, set artifactid to exec-maven-plugin and apply a lean setup that invokes a mainClass via exec:java. This will keep your build deterministic, and the saved configuration travels with the project.
Define the required parameter mainClass and the optional parameters like arguments, systemProperties, and workingDirectory. The plugin reads these from the configuration, and you can reuse a single setup for multiple modules.
Below is a concrete usage path: add the plugin to the build, set artifactid to exec-maven-plugin and specify groupId, version, and an execution to run exec:java. Use the atributo mainClass to name the entry point and supply the parameters to pass input. For dynamic values, lookup configuration from httpwwwbarcomquery and feed them into the process. This approach pairs well with dropwizard apps that read config on startup from the bundle.
From there, you can compare saved time against the other approaches and reuse the same instance across modules. The component schemas and attribute offsets stay consistent, with the setup below to help teams move fast.
Set up JDK, Maven, and environment for the Exec Maven Plugin
Install JDK 17 LTS and Maven 3.9.x, then set JAVA_HOME and PATH and verify with java -version and mvn -v.
JDK and Maven installation
Linux (Debian/Ubuntu): sudo apt-get update && sudo apt-get install -y openjdk-17-jdk; export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64; export PATH=$JAVA_HOME/bin:$PATH; java -version; mvn -v
macOS: brew install openjdk@17; echo 'export JAVA_HOME="/usr/local/opt/openjdk@17"' >> ~/.zshrc; source ~/.zshrc; java -version; mvn -v
Windows: install OpenJDK 17, set JAVA_HOME in System Properties, add %JAVA_HOME%bin to PATH; download Maven binary, extract to C:maven, set M2_HOME and update PATH; verify with java -version and mvn -v
Exec Maven Plugin configuration and usage
Declare the plugin with groupId org.codehaus.mojo, artifactId exec-maven-plugin, version 3.1.0 to ensure compatibility with JDK 17 and modern Maven features. This choice aligns with the maven-plugin-plugin implementation and keeps the build lightweight.
Prepare environment mapping for target instances under different settings and load a single set of arguments via -Dexec.mainClass and -Dexec.args. Use a single -Dexec.args string to pass parameters; an arrays approach can help when you need multiple options, while keeping the launcher concise. The prefix for -Dexec.* properties clarifies which values belong to the main class and which belong to the classpath, and a clean mapping avoids killing the shell during long runs.
Run patterns and troubleshooting: mvn exec:java -Dexec.mainClass=com.example.Main -Dexec.args="start yyyy-mm-dd arg1" binds to the runtime phase and executes a single instance. For a packaging phase, you can attach to a phase like package via executions in the pom, then invoke mvn package to trigger the exec:java step with -Dexec.classpathScope=runtime. To load specific project settings, use -s /path/settings.xml. For quick reference, you might perform a search-like check at httpwwwbarcomquery to confirm documentation alignment and adjust the query or mapping as needed. If you maintain deprecated scripts, note that orgapacheant references may appear in legacy tasks and should be migrated gradually to the Exec Maven Plugin model.
Add a minimal plugin section to pom.xml for a quick start
Minimal plug-in configuration
To employ a minimal plug-in section for a quick start, add under build/plugins a single exec-maven-plugin configuration. Bind the execution to the post-integration-test phase and use a small prefix such as 'demo' to keep the scope clear. This introduction is intended for newcomers and keeps the pom saved with a lean footprint. Previously, teams relied on verbose boilerplates; this contrast shows how a compact configuration maps to a clean lifecycle. The plugin will start after tests, and a concise notice appears in the log when it runs. The execution is mapped to the post-integration-test phase. Documentation explains the required parameters; maps describe how execution goals relate to lifecycle steps, and the processes remain simple. The orgcodehausmojo pattern is referenced, and the source of truth is the official documentation, источник.
Validation and next steps
After adding, run mvn verify. The execution started and the plug-in will run during post-integration-test as configured; you will notice the log entry and the saved artifacts (if any). If you want to test another module, copy the same block with a different id; this keeps the processes isolated and enables easy comparison. This approach offers a clear contrast to larger configurations and supports quick validation. Documentation covers how to adjust the prefix and goals; maps define which phase triggers which execution, and the overall process remains lightweight. The источник for this practice remains the official docs, and you'll see that the setup will scale to multiple projects with consistent results.
Configure executable, arguments, and working directory for a sample task
Set executable, arguments, and workingDirectory in the Exec Maven Plugin configuration to guarantee predictable results across environments. Start with a simple, explicit setup for the sample task to avoid ambiguity.
Keep a single execution (id: sample-run) and define it as an object with three fields: executable, arguments, and workingDirectory. These three fields form small objects. This avoids collectionsarrays and reduces the risk of mis-binding. Some teams commonly adopt this pattern. The mechanism follows the standard execution path and is easy to rebind if paths change.
For example: executable: /bin/bash; arguments: -lc "echo health; exit 0"; workingDirectory: ${project.basedir}/scripts. The plugin coordinates use artifactid of exec-maven-plugin so the correct mechanism is enabled. For some projects, run on Windows (cmd.exe) or Unix shells; adjust executable and arguments accordingly.
Use properties to keep values portable: define them in the pom or via -D flags, and bind them to executable, arguments, and workingDirectory in a fully portable setup. They move with the project, avoiding drift between modules and builds. This is the configuring approach you’ll repeat for other tasks.
Validation remains straightforward: run mvn -q package and observe the console for the command output; the executions section should show a single run with the defined executable. If you need more runs, extend the object, but keep the configuration flat to reduce complexity. For deeper guidance, search videos and searches that illustrate a clean, repeatable pattern.
Run a simple command via Maven to verify setup and capture exit status
Choose the Exec Maven Plugin command below to verify setup and capture the exit status in one run. Use the plugin-prefix to invoke this quick check inside CI and local shells. This small check creates a single object representing a basic operation; the exit code indicates success or failure, and the time spent is measured in seconds for quick feedback. These checks were designed to run without external dependencies. For dropwizard projects, you can adapt this to verify the startup phase and then query an endpoint such as httpwwwbarcomquery to confirm the service is reachable. These steps are documented as a collection array and can be organized under a plugingroup named 'employ' to keep tests tidy, with several optional certificates for TLS if needed. These checks are designed to be extended with additional fields like code and fieldgetgenerictype below.
Run this exact command in your terminal to execute a simple echo and return code 0: mvn org.codehaus.mojo:exec-maven-plugin:3.1.0:exec -Dexec.executable=bash -Dexec.args='-lc "echo OK; exit 0"'
Command details
The command uses the plugin-prefix 'exec' and was designed to work across environments with minimal setup. It employs a single shell and prints a message, representing a minimal collection item that can be extended with additional fields like code, phase, or optional certificates in a real test model.
Results interpretation
Zero exit means the Maven invocation and the inner command both succeeded. Non-zero indicates a break in the setup or in the command execution. In CI, capture the exit status in the current job step and map it to a boolean success flag for downstream checks. The below table shows representative outcomes and how to read them.
| Command | Description | Exit status | Notes |
|---|---|---|---|
| mvn org.codehaus.mojo:exec-maven-plugin:3.1.0:exec -Dexec.executable=bash -Dexec.args='-lc "echo OK; exit 0"' | Prints OK and returns 0 | 0 | Success path; seconds to execute, object representing a simple check; plugin-prefix used |
| mvn org.codehaus.mojo:exec-maven-plugin:3.1.0:exec -Dexec.executable=bash -Dexec.args='-lc "echo FAIL; exit 1"' | Prints FAIL and returns 1 | 1 | Failure path; adjust configuration or environment |
| mvn org.codehaus.mojo:exec-maven-plugin:3.1.0:exec -Dexec.executable=bash -Dexec.args='-lc "sleep 2; echo done; exit 0"' | Delays a moment then prints done | 0 | Demonstrates handling of seconds; suitable for timing checks |
Tag vs Tag: evaluate when to use each tagging approach in build steps
Recommendation: Tag on commit for durable cross-project snapshots; otherwise, tag at build steps to mark milestones in CI. Always align tagging with your documentation and rules, and define clear criteria so teams know when to apply each tag type.
Commit-based tagging anchors a tag to a specific commit, creating a stable reference that travels with the project across classes and projects. This approach is ideal when you need a single source of truth that other teams can search and merge against, even after they started new work. Use a consistent naming model like project-yyyy-mm-dd or project-
Build-step tagging attaches a tag to the build itself rather than to a commit, which suits milestone builds and internal testing. Apply it at a phase boundary, such as after a phase completes or when a specific goalstartgoal finishes, so the tag reflects the exact build output without touching source history. This approach keeps the commit history clean while still giving you a traceable artifact lineage. You can define optional tags for experimental runs and reserve mandatory tags for released artifacts, with seconds precision in log timestamps to improve traceability in the build log.
Rules for choosing between them: use commit tagging when you must coordinate multiple projects that must align on the same source state; use build-step tagging when you want rapid feedback and minimal SCM churn. In either case, document tag naming conventions, the tag lifecycle, and how teams should search for their tags. If a tag is merged across projects, they should be able to locate it by project, date, and phase, then verify the instance and their environment against the tag. You can start with a project-wide policy and extend it per class or module as needed, ensuring each project follows the same process and uses the same plugin-prefix to create tags through the SCM tooling.
Redirect and log plugin output for debugging and traceability
Starting with a simple directive, redirect plugin output to a dedicated file and merge streams to ensure a complete, searchable trace.
Configuration patterns
- starting with a logs directory under ${project.build.directory}/logs and ensure it exists before executions; this keeps files organized and predictable.
- define a single, named log file per execution, for example target/logs/exec-20250101-120000.log, so completion status and errors appear in one place.
- enable redirectErrorStream so stdout and stderr appear in the same section of the file, simplifying searching and analysis.
- use a directory attribute for module paths and map each execution's line to a field that serves as an attribute identifying the source.
- inject a simple expression to compute the log path from properties, e.g., ${project.build.directory}/logs/exec-${mojo.timestamp}.log, keeping the setup defined and reusable.
- include references to related resources in a log header, such as a httpwwwbarcomquery link for quick access to a related bound query, like myboundquerymojo; this lets users jump from logs to the commit details.
- record the commit or change context when you run myboundquerymojo, so the report shows which changes triggered the executions.
Traceability and reporting considerations
- store structured entries for elements, attribute values and rules, so you can search by field or value across executions.
- collect and group data into arrays and collectionsarrays for post-processing in CI dashboards.
- keep a single source of truth by always including a summary block at the top of the log with completion status and a quick reference to the files created.
- link to the report directory and a sample report path, e.g., target/logs/reporting.html, to make it easy for the team to review results.
- keep the log naming deterministic so searching across runs remains simple.
Incorporate the plugin into a CI workflow for repeatable builds
Always run the plug-in in a clean CI job so each build starts from a known state. Use a single, explicit mvn command that executes verify and triggers the Exec Maven Plugin to perform a lightweight environment check before tests. This approach ensures nothing in the workspace leaks between sessions, and it locks down the dependency and Java/Maven versions used by the build. With a fixed CLI and a deterministic plugin configuration, you refer to a true signal about the environment rather than ad‑hoc guesses. If a baseline mismatch is detected, fail the job immediately rather than continuing, reducing wasted seconds and rework.
Configuring the plug-in for repeatable builds
Configure the plug-in in the root pom so inside every project the same mechanism runs. Define a single execution that triggers during the validate phase and uses exec:java to bootstrap a tiny, deterministic check. The check prints a fixed fingerprint derived from the Java version, Maven version, operating system type, and a hash of the provided dependency set; if the fingerprint differs from the given baseline, the build would fail. This mechanism avoids coercion of values and eliminates any risk of a different mapping of environment types between projects. Use mappings that tie CI inputs (like JAVA_HOME and OS name) to plugin parameters, so the second run mirrors the first exactly. Ensure the check runs without touching files in the main sources and that it executes inside a dedicated session so each project starts with a clean slate.
Operational tips for CI
Place CI steps so the plug-in runs before the full test suite. The first step validates the environment; the second runs tests with the same classpath and dependency set, ensuring repeatable results. Keep the command line simple: a single mvn -B -DskipITs=false clean verify, with the plug-in configured to execute a minimal Java class that outputs a stable code signature. Refer to the mappings in the plugin configuration to guarantee that the test scope and the verified scope use the same dependencies and plugin goals. Use a dedicated CI image that provides the exact types of tools you rely on, otherwise drift will creep in. In multi‑module projects, this approach scales because each module inherits the same plug‑in execution, inside the same session, with a predictable start time. If a project includes optional modules, set the plugin to ignore them unless their presence is required by the dependency graph, otherwise the build could diverge. Track seconds spent on environment validation separately from test execution, so you can quantify how long the initial checks take and optimize the CI pipeline accordingly. When a failure occurs, provide a concise log with the exact mappings used and the fingerprint that caused the failure, enabling quick remediation and a repeatable deal between code and test environments.




