Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong resources directory path #2880

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
Expand All @@ -37,7 +38,14 @@
public class JavaProject implements Serializable {

private static final long serialVersionUID = 6438404976521622633L;


/**
* Properties file for the project
*
* @param propertiesFile for the project.
* @return Properties file for the project.
*/
private URL propertiesFile;
/**
* Project's name.
*
Expand Down Expand Up @@ -80,6 +88,13 @@ public class JavaProject implements Serializable {
* @return The base directory for the project.
*/
private File baseDirectory;
/**
* Project's resources directory
*
* @param resourcesDirectory resources directory where project resources look for.
* @return The resources directory for the project.
*/
private File resourcesDirectory;
/**
* Directory where all build files are located (e.g. target)
*
Expand Down Expand Up @@ -293,6 +308,7 @@ public JavaProject(
this.scmTag = scmTag;
this.buildPackageDirectory = buildPackageDirectory;
this.maintainers = maintainers;
this.resourcesDirectory = baseDirectory != null ? new File(baseDirectory.getAbsolutePath() + "/src/main/resources") : new File("src/main/resources");
}
}

@@ -0,0 +1,16 @@
package org.eclipse.jkube.kit.common;

import java.net.URL;
import java.util.Properties;

public class PropertiesExtender extends Properties {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the motivation behind using this POJO. Why not use a URL field directly?

private URL propertiesFile;

public void setPropertiesFile(URL file) {
this.propertiesFile = file;
}

public URL getPropertiesFile() {
return this.propertiesFile;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using Lombok, You can directly use @Getter/@Setter here.

}
Expand Up @@ -175,7 +175,8 @@ public static String createDefaultResourceName(String artifactId, String ... suf
public static URLClassLoader getClassLoader(JavaProject jKubeProject) {
return ClassUtil.createClassLoader(
jKubeProject.getCompileClassPathElements(),
jKubeProject.getOutputDirectory().getAbsolutePath());
jKubeProject.getOutputDirectory().getAbsolutePath(),
jKubeProject.getResourcesDirectory().getAbsolutePath());
}

public static String getProperty(String key, JavaProject project) {
Expand Down
Expand Up @@ -16,6 +16,7 @@
import lombok.Builder;
import lombok.Getter;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.PropertiesExtender;

import java.util.Optional;
import java.util.Properties;
Expand All @@ -37,9 +38,11 @@ public class SpringBootConfiguration {
private String actuatorDefaultBasePath;

public static SpringBootConfiguration from(JavaProject project) {
final Properties properties = SpringBootUtil.getSpringBootApplicationProperties(
final PropertiesExtender properties = SpringBootUtil.getSpringBootApplicationProperties(
SpringBootUtil.getSpringBootActiveProfile(project),
JKubeProjectUtil.getClassLoader(project));
project.setPropertiesFile(properties.getPropertiesFile());

final int majorVersion = SpringBootUtil.getSpringBootVersion(project)
.map(semVer -> {
try {
Expand Down
Expand Up @@ -23,6 +23,7 @@

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;
import org.eclipse.jkube.kit.common.PropertiesExtender;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource;

Expand Down Expand Up @@ -64,14 +65,22 @@ public static Properties getSpringBootApplicationProperties(URLClassLoader compi
* @param compileClassLoader compile class loader
* @return properties object
*/
public static Properties getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
public static PropertiesExtender getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) {
URL ymlResource = compileClassLoader.findResource("application.yml");
URL propertiesResource = compileClassLoader.findResource("application.properties");

Properties props = YamlUtil.getPropertiesFromYamlResource(springActiveProfile, ymlResource);
props.putAll(getPropertiesFromResource(propertiesResource));
return new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
.replaceAllPlaceholders(props);
props = new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true)
.replaceAllPlaceholders(props);

// Extend Properties object with resources file path
PropertiesExtender propsExtender = new PropertiesExtender();
URL propertiesFile = ymlResource != null ? ymlResource : propertiesResource;
propsExtender.setPropertiesFile(propertiesFile);
propsExtender.putAll(props);

return propsExtender;
}

/**
Expand Down
Expand Up @@ -60,6 +60,7 @@ void setUp(@TempDir Path target) throws IOException {
project = JavaProject.builder()
.properties(properties)
.outputDirectory(target.toFile())
.baseDirectory(target.toFile())
.build();
}

Expand Down
Expand Up @@ -99,6 +99,7 @@ void getSpringBootApplicationProperties_withCompileClassloader_shouldLoadPropert
JavaProject javaProject = JavaProject.builder()
.compileClassPathElement(Objects.requireNonNull(getClass().getResource("/util/springboot/resources")).getPath())
.outputDirectory(new File("target"))
.baseDirectory(new File("target"))
.build();
URLClassLoader compileClassLoader = JKubeProjectUtil.getClassLoader(javaProject);

Expand Down
Expand Up @@ -57,6 +57,7 @@ public List<ImageConfiguration> generate(List<ImageConfiguration> imageConfigs)
if (generator.isApplicable(ret)) {
log.info("Running generator %s", generator.getName());
ret = generator.customize(ret, genCtx.isPrePackagePhase());
log.info("The following properties file is used %s", genCtx.getProject().getPropertiesFile());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.info("The following properties file is used %s", genCtx.getProject().getPropertiesFile());
log.info("The following properties file are used %s", genCtx.getProject().getPropertiesFile());

}
}
return ret;
Expand Down
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.config.image.GeneratorManager;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
Expand All @@ -43,6 +44,7 @@ void setUp() {
GeneratorContext generatorContext = GeneratorContext.builder()
.config(processorConfig)
.logger(logger)
.project(new JavaProject(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed? How is this change verifying the bug this patch is fixing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can directly use JavaProject.builder().build()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your very valuable feedback! I take into account your remarks.

.build();
generatorManager = new DefaultGeneratorManager(generatorContext);
}
Expand Down