use anyhow::Result;
use crate::core::session::MavenSession;
use crate::core::project::MavenProject;
use crate::core::lifecycle::LifecyclePhase;
use crate::core::mojo_executor::{MojoExecutor, MojoExecution};
use crate::plugin_api::registry::PluginRegistry;
pub struct LifecycleExecutor {
mojo_executor: MojoExecutor,
}
impl LifecycleExecutor {
pub fn new() -> Self {
Self {
mojo_executor: MojoExecutor::new(),
}
}
pub fn execute_phase(
&self,
session: &MavenSession,
project: &MavenProject,
phase: &LifecyclePhase,
) -> Result<()> {
tracing::info!("Executing phase: {} for project {}", phase, project.id());
let mojo_executions = self.get_phase_mojos(project, phase)?;
self.mojo_executor.execute(session, &mojo_executions)?;
Ok(())
}
fn get_phase_mojos(
&self,
_project: &MavenProject,
phase: &LifecyclePhase,
) -> Result<Vec<MojoExecution>> {
let mut executions = Vec::new();
match phase {
LifecyclePhase::Compile => {
executions.push(MojoExecution {
group_id: "org.apache.maven.plugins".to_string(),
artifact_id: "maven-compiler-plugin".to_string(),
version: None, goal: "compile".to_string(),
phase: Some("compile".to_string()),
configuration: None,
});
}
LifecyclePhase::TestCompile => {
executions.push(MojoExecution {
group_id: "org.apache.maven.plugins".to_string(),
artifact_id: "maven-compiler-plugin".to_string(),
version: None,
goal: "testCompile".to_string(),
phase: Some("test-compile".to_string()),
configuration: None,
});
}
LifecyclePhase::Test => {
executions.push(MojoExecution {
group_id: "org.apache.maven.plugins".to_string(),
artifact_id: "maven-surefire-plugin".to_string(),
version: None,
goal: "test".to_string(),
phase: Some("test".to_string()),
configuration: None,
});
}
LifecyclePhase::Package => {
executions.push(MojoExecution {
group_id: "org.apache.maven.plugins".to_string(),
artifact_id: "maven-jar-plugin".to_string(),
version: None,
goal: "jar".to_string(),
phase: Some("package".to_string()),
configuration: None,
});
}
LifecyclePhase::Install => {
executions.push(MojoExecution {
group_id: "org.apache.maven.plugins".to_string(),
artifact_id: "maven-install-plugin".to_string(),
version: None,
goal: "install".to_string(),
phase: Some("install".to_string()),
configuration: None,
});
}
LifecyclePhase::Deploy => {
executions.push(MojoExecution {
group_id: "org.apache.maven.plugins".to_string(),
artifact_id: "maven-deploy-plugin".to_string(),
version: None,
goal: "deploy".to_string(),
phase: Some("deploy".to_string()),
configuration: None,
});
}
_ => {
}
}
Ok(executions)
}
pub fn execute_to_phase(
&self,
session: &MavenSession,
project: &MavenProject,
target_phase: &LifecyclePhase,
) -> Result<()> {
let all_phases = LifecyclePhase::all();
for phase in all_phases {
if phase == *target_phase {
self.execute_phase(session, project, &phase)?;
break;
}
self.execute_phase(session, project, &phase)?;
}
Ok(())
}
pub fn with_plugin_registry(self, _registry: PluginRegistry) -> Self {
self
}
}
impl Default for LifecycleExecutor {
fn default() -> Self {
Self::new()
}
}