I have profiles: dev, prod.
And my homepage located at /src/main/resources/static/index.html
How to make different homepage with different profile?
For example, /src/main/resources/static-dev/index.html and /src/main/resources/static-prod/index.html.
Any advice?
Finally I got a simple solution.
Use different config file application.properties and application-prod.properties.
Each of them I config a different resource location. For example spring.resources.static-locations=classpath:/static-dev/.
If your project supports the Maven dependency manager, Maven's build profiles may be able to help you:
<profile>
<id>live</id>
<properties>
<environment>live</environment>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/${environment}</directory>
</resource>
</resources>
</build>
</profile>
The code above should be in your pom.xml. In your Spring properties you can specify the active profile in one line:
spring.profiles.active=live
This should be enough to conditionally load any resources.
Both resources should be put under /src/main/resources/static (since this is the default static resource folder IIRC) and then categorized into /prod and /dev. Then in your #GetMapping controller, choose to return /prod/index or /dev/index based on your condition
You can create a Filter that changes the request URL from /index.html to /dev/index.html or /prod/index.html as needed.
The filter can also do the /dev or /prod prefixing for .css and .js files.
Unless all your files are split between dev and prod, you'd probably need an explicit list of which requests should be prefixed.
Related
I have the following properties in the pom file
<name>DemoApplication</name>
<description>Demo spring project</description>
<version>1.0.0-SNAPSHOT</version>
And I have a class that reads the properties from application.yml
But instead of using the application.yml under src/main/resources I am specifying the properties through an external file as follows
java -jar DemoApplication-1.0.0-SNAPSHOT.jar --spring.config.location=application.yml
In this external application properties, I have the following attributes
swagger:
apiTitle: '#project.name#'
apiDescription: '#project.description#'
apiVersion: '#project.version#'
The issue is that the #project.name# and other properties are not being replaced as expected, but are read as-is.
How should the problem be approached?
According that section of the official documentation of Spring Boot v2, you can configure it with :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
With useDefaultDelimiters set to false or to true depending on your configuration.
The others sections of that official documentation will be helpful for your use case, especially these one : "77.5 Use YAML for External Properties".
If nothing is working, why don't you are loading a custom Properties file ? It could be loaded as you need without any problem. Just reference it with the correct path when you are starting your program, and inside your program, test if your file config.properties is available and contains what you need to work with.
Of course, the Maven way of loading resources files is the best easy way to go, and it should be a simple Properties file too. I have done exactly that way inside the software I am released to manage my configuration :
Writing a app.properties
Loading that file with Maven at runtime with resource configuration
Expanding properties with classical syntax ${my.prop}
Run the program with a Maven task.
Of course, when you distribute your app as a jar, it is a bit different.
Maybe you can try to write your properties files within a Maven goal.
maven:
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<active.spring.profile>local</active.spring.profile>
</properties>
</profile>
</profiles>
application.properties:
spring.profiles.active=#active.spring.profile#
spring.config.additional-location=classpath:/profile/application-${spring.profiles.active}.properties
and after this I can't get value from src/main/resources/application-local.properties which contains test.prop=123
#Service
public class TestProps {
#Value("${test.prop}")
String testProp;
#PostConstruct
void run() {
System.out.println(testProp);
}
}
Where is the mistake? or it's a bug?
The property: spring.config.additional-location has to be provided as argument for JVM like this: java -Dspring.config.additional-location=classpath:/profile/application-local.properties -jar whatever.jar.
Doesn't make sense to have it in application.properties. From the documentation:
Alternatively, when custom config locations are configured by using
spring.config.additional-location, they are used in addition to the
default locations. Additional locations are searched before the
default locations.
Because the additional locations are searched before the default locations, they have to be provided earlier, so you can't have them in application.properties
additional-location really does not belong to application.properties, because Spring will not interpret it from there. It needs to load the additional config files before the default ones.
On the other hand, it is handy to set the default additional location in maven spring boot plugin. This way one would be able to set security-sensitive information outside of the project sources and it will not appear in the source repository. For example, the client security config could read:
app:
client:
ssl:
keystore: path/to/keystore
keystore-password: the-keystore-password
key-password: the-key-password
Default application.yml in src/main/resources can be skipped, while the additional config could provide the values per active profile and keep them safe on the local machine.
I'd like to use values from a properties file (or some other filesystem resource) in my weblogic.xml. For example, I have this section:
<session-descriptor>
<cookie-name>JSESSIONID</cookie-name>
<cookie-domain>${my.domain}</cookie-domain>
</session-descriptor>
I then have a properties file specifying the value:
my.domain=qa.mydomain.com
on the file system specifying the domain.
Is this possible? Many other configuration mechanisms allow for this. The motivation is that the same code could be deployed in multiple environments with multiple domains and weblogic could simply take the appropriate domain from the file without any operator intervention.
Running weblogic 12c here.
Thanks!
This can be simply achieved using maven's resource plugin, assuming you already on mvn build.
You just need to add below configuration under <build> section
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
You can add below property in your main pom in respective profiles:
<my.domain>desiredValue</my.domain>
In my opinion what you need is to use Weblogic's Deployment Plan feature.
I'm not so familar with it (never used it in productive environments) but with a deployment plan you should be able to change values in web.xml/weblogic.xml during deployment time.
Docs/Example:
Oracle Help Center - Creating and Using a Deployment Plan
Oracle Docs - Save Deployment Plan
Example from middlewaremagic.com
I have a maven project with different profiles set in pom.xml with different values. But I don't know how to access those values set in profile via java code.
For example-
My pom.xml:
<profile>
<id>scaler</id>
<properties>
<user>xxxxxxx</user>
<secret>yyyyyyyy</secret>
<proxyHost>172.19.17.13</proxyHost>
<proxyPort>9444</proxyPort>
<environment>SCALER</environment>
</properties>
</profile>
Java code-
String serviceurl = "http://"<proxyhost>":<proxyPort>/";
In the above java code, i want to use proxy host as 172.19.17.13 & port as 9444 as defined in pom.xml but how to access those values from pom??
I will appreciate your help
You should use the maven filtering feature.
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
Just add a property file in src/main/resources with some placeholders:
key=${myvalue}
then myvalue should be defined as a property in your pom.xml
Be sure to activate the filter on your resources:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
I'm not sure it depends on maven profile. You can try to use properties-maven-plugin (or other solution) like it described here. Just to write your properties into file and then use it in java code.
Let's assume I have a Java project called com.example:awesome-project, which has a dependency called com.external:awesome-library.
If this awesome-library has resources like configuration.xml, how do I filter this resource so that Maven property placeholders are replaced properly before I use it in the project's code?
Things like <tag name="${groupId}:${artifact}"> need to be changed to <tag name="com.example:awesome-project">, for instance.
Assumptions:
The awesome-library is external to this project, and is not built by
me.
I have no knowledge about the variable placeholders used in the
.xml files. All I know is that they are defined somehow by Maven.
Let's take project meta-data, for example ${artifact}, as an example.
Use dependency:unpack, which will extract resources, by default to ${project.build.directory}/dependency (which you can change if you want).
Then, either use either:
resources:copy-resource and define your filtering in that plugin's execution and also configure its resources configuration to point to ${project.build.directory}/dependency
it might be simpler to just put a resources element in your POM's build section and point it to ${project.build.directory}/dependency with appropriate filtering. Though if you want the standard src/main/resources as well you will need to put it there also.
You can use this solution:
Create parent (builder) project, and place in it constant which will be replaced in resource file:
<properties>
<some.constant>123</some.constant>
</properties>
Define both projects as modules in parent project:
<modules>
<module>../awesome-project</module>
<module>../awesome-library</module>
</modules>
In my-configuration.xml place constant reference as
${some.constant}
In awesome-library.pom config filter processor (for example, suppose that my-configuration.xml is placed in src/main/resources/META-INF:
<build>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
<targetPath>META-INF</targetPath>
<filtering>true</filtering>
<includes>
<include>**/my-configuration.xml</include>
</includes>
</resource>
Run maven clean install in parent project.