in sources/amper-cli/src/org/jetbrains/amper/tasks/maven/ExecuteMavenMojoTask.kt [77:164]
override suspend fun run(
dependenciesResult: List<TaskResult>,
executionContext: TaskGraphExecutionContext,
): TaskResult {
val projectEmbryo = dependenciesResult
.filterIsInstance<MavenPhaseResult>()
.singleOrNull()?.embryo
?: MavenProjectEmbryo()
val localRepoPath = MavenLocalRepository.Default.repository
val repoSession = plexus.createRepositorySession(localRepoPath)
val request = plexus.mavenRepositorySystem.createMavenExecutionRequest(localRepoPath).apply request@{
// Install remote artifact repositories.
module.mavenRepositories.forEach {
if (!it.isMavenLocal) addRemoteRepository {
plexus.repoSystem.createDefaultRemoteRepository(this@request).apply {
id = it.id
url = it.url
if (it.userName != null && it.password != null) addServer {
username = it.userName
password = it.password
}
}
}
}
}
val newMavenProject = MockedMavenProject(mavenProject).apply {
file = module.source.buildFile.toFile()
build.directory = buildOutputRoot.path.absolutePathString()
artifact = module.asMavenArtifact("runtime")
model.dependencyManagement = MavenDependencyManagement()
// Get all collected configurations from the embryo.
projectEmbryo.configureProject(this)
// Set artifact repositories that are usually being set within the model building listener.
remoteArtifactRepositories = request.remoteRepositories
// Set plugin repositories to the maven central.
this@apply.pluginArtifactRepositories = listOf(plexus.repoSystem.createDefaultRemoteRepository(request))
}
val configDom = Xpp3DomBuilder.build(configString.reader())
val mojoDesc = plexus.mavenPluginManager.getMojoDescriptor(
/* plugin = */ mavenPlugin,
/* goal = */ mojo.goal,
/* repositories = */ newMavenProject.remotePluginRepositories,
/* session = */ repoSession,
)
val session = MavenSession(
/* container = */ plexus,
/* repositorySession = */ repoSession,
/* request = */ request,
/* result = */ DefaultMavenExecutionResult()
).apply { currentProject = newMavenProject }
val mojoExecution = MojoExecution(
/* mojoDescriptor = */ mojoDesc,
/* executionId = */ executionContext.executionId,
/* source = */ MojoExecution.Source.CLI
).apply { configuration = configDom }
// Basically this is needed to add default parameters to the configuration.
plexus.lifecycleExecutionPlanCalculator.setupMojoExecution(session, mavenProject, mojoExecution)
// Execute mojo.
spanBuilder("Executing maven plugin mojo: $pluginCoordinates:${mojo.goal}").use {
// We need to enter the session, since maven creates a Guice session for
// session scoped named beans each time a maven execution request is processed.
plexus.sessionScope.use {
seed(MavenSession::class.java, session)
executeMojoOrPerformReport(
session = session,
repoSession = repoSession,
mojoExecution = mojoExecution,
remoteRepositories = request.remoteRepositories
)
}
}
return ModelChange(
newMavenProject.newSourceRoots.map(::relativeToBase),
newMavenProject.newTestSourceRoots.map(::relativeToBase),
)
}