How to get config data from local.properties to im

2020-07-17 07:26发布

问题:

Is it possible to get a value from the environment variables defined at local.properties configuration file and access it via the impex file?

Ex.

$someMacro=<some variable from config>

Thank you!

回答1:

You can add this to your impex:

# Import config properties into impex macros
UPDATE GenericItem[processor=de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor];pk[unique=true]

All your configurations from local.properties, etc. are now loaded and can be used via $config- prefix, say for example:

local.properties

your.config.property=322

So your impex would look something like:

# Import config properties into impex macros
UPDATE GenericItem[processor=de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor];pk[unique=true]

$variable=$config-your.config.property

INSERT_UPDATE SampleItem;code[unique=true];name
;sample1;$variable

# OR you can just directly use the config macro
INSERT_UPDATE SampleItem;code[unique=true];name
;sample1;$config-your.config.property

Hope this works for you.

EDIT: Please also note that if there was no such property found, the value stored on the sample above shall be exactly: $config-your.config.property.



回答2:

To complete @Atsusa Kai answer, the line with a header alone can be avoided.

It's quite ugly to have this line just to load the properties... but that's actually what is mentionned in the ConfigPropertyImportProcessor class comment :

  /**
   * Impex ImportProcessor that injects all config properties as impex definitions.
   * All defined configuration properties are added as impex macro definitions with
   * the prefix of "config-". For example the config key <tt>mail.smtp.server</tt>
   * can be accessed via the macro <tt>$config-mail.smtp.server</tt>.
   * In order to use this import processor and to load the configuration properties
   * the following must be added to the top of the impex file:
   *
   * <tt>UPDATE GenericItem[processor=de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor];pk[unique=true]</tt>
   */

The alternative is to used a beanshell command that are tailored for this kind of action.

You can replace the UPDATE GenericItem line by

#%new de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor().init(impex)

but you need to enable code execution.



标签: hybris impex