XML์ Visual Studio์ ๋ด์ฅ๋ XDT ๋ณํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํตํด ์ ๋ฒ์ ์ผ๋ก ๋ณํํ ์ ์์ต๋๋ค.
๋ถ์ธ ์ฑ๋ช ํ๊ตญ์ด ์ค๋ ฅ์ด ๋ถ์ ํ์ฌ ์ด ๊ธ์ด ๊ตฌ๊ธ ๋ฒ์ญ๊ธฐ๋ฅผ ์ฃผ๋ก ํ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ถ์ ํํ ๋ฌธ๋ฒ๊ณผ ์ดํ๊ฐ ์์์ ์์ต๋๋ค. ์ด ์ ์ํด ๋ถํ๋๋ฆฌ๋ฉฐ, ์ถํ์ ๋ค์ ๊ฒํ ํ์ฌ ์์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
๋๋ณธ
๋ค์๊ณผ ๊ฐ์ XML ์ค์ ํ์ผ์ด ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
<website>
<title>๋ด ์ฌ์ดํธ</title>
<environment>๊ฐ๋ฐ</environment>
<url>http://localhost:8080</url>
</website>์ด ์ค์ ์ ๋ก์ปฌ/๊ฐ๋ฐ ํ๊ฒฝ์๋ง ์ ์ฉ๋ฉ๋๋ค. ํ๋ก๋์ ํ๊ฒฝ์์ ์ฌ์ฉํ๋ ค๋ฉด ์ค์ ์ ๋ณต์ฌํ์ฌ ๋ถ์ฌ๋ฃ๊ณ ๊ด๋ จ ์์ฑ๋ง ํ๊ฒฝ์ ๋ง๊ฒ ๋ณ๊ฒฝํ๋ฉด ๋ฉ๋๋ค.
<website>
<title>๋ด ์ฌ์ดํธ</title>
<environment>์์ฐ</environment>
<url>https://www.mysite.com</url>
</website>๊ฐ๋จํ ์ค์ ์ด๋ผ๋ฉด ๊ด์ฐฎ์ ์ ์์ต๋๋ค. ํ์ง๋ง ์ค์ ์ด 1,000์ค์ด๋ ๋๋ค๋ฉด ์ด๋จ๊น์? ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ๋ฐฉ์์ ์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅ์ฑ์ด ๋๊ณ ์ ์ง ๊ด๋ฆฌ๊ฐ ์ด๋ ค์์ง๋๋ค. ๋ํ ์ผ๋ถ ์์ฑ์ ์ฌ๋ฌ ํ๊ฒฝ์์ ์ฌ์ฌ์ฉ๋๊ณ ๊ณต์ ๋๋ฏ๋ก ๋ชจ๋ ์์ฑ์ ๋ณ๊ฒฝํ ํ์๊ฐ ์์ด ์ค๋ณต์ด ๋ฐ์ํฉ๋๋ค.
๋ฐ๋ผ์ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ๋์ XDT ๋ณํ์ ์ฌ์ฉํ์ฌ ๊ธฐ๋ณธ XML ํ์ผ์ ํ์ฅํ๋ ๊ฒ์ด ๊ฐ๋ฅํฉ๋๋ค.
๊ตฌ๋ฌธ ์๊ฐ
XDT ๋ณํ์ ์ด๋ ๊ฒ ์๊ฒผ์ต๋๋ค. ์ฌ์ ํ XML ํ์ผ์ ๋๋ค.
<?xml version="1.0" encoding="utf-8" ?>
<website xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<environment
xdt:Locator="XPath(//website/environment)"
xdt:Transform="Replace">์์ฐ</environment>
<url
xdt:Locator="XPath(//website/url)"
xdt:Transform="Replace">https://www.mysite.com</url>
</website>์ฌ๊ธฐ์ title์ ๋ชจ๋ ํ๊ฒฝ ์ถ๋ ฅ์์ โโ๋์ผํ๋ฏ๋ก ๋ฌด์ํ๊ณ environment์ url๋ง ๋ณํํฉ๋๋ค. xdt:Locator๋ "์๋ณธ ์์์ ์์น"๋ฅผ ๋ํ๋ด๊ณ xdt:Transform์ "์ด๋ป๊ฒ ์กฐ์ํ ๊ฒ์ธ๊ฐ"๋ฅผ ์ง์ ํฉ๋๋ค. ์ด๊ฒ๋ค์ด XDT ๋ณํ์ด ์ ๊ณตํ๋ ๊ธฐ๋ณธ์ ์ธ ์ฐ์ฐ์์
๋๋ค.
ํต์ฌ ์์ด๋์ด๋ prod.config.xml ํ์ผ์ด dev.config.xml ํ์ผ์ ๊ธฐ๋ฐ์ผ๋ก XDT ๋ณํ์ ๊ฑฐ์ณ ๋ง์น ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ์ฒ๋ผ ์์ ํ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง ๊ฒฐ๊ณผ๋ฌผ์ ์์ฑํ๋ ๊ฒ์
๋๋ค. ๋ค์ ๋จ๊ณ์์๋ ๋ณํ ํ๋ก์ธ์ค๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
ํ๋ก์ ํธ ์ค์
๋จผ์ Visual Studio์์ MyXmlTransforms๋ผ๋ ์ด๋ฆ์ ๋น ์ฝ์ ํ๋ก์ ํธ๋ฅผ ์์ฑํฉ๋๋ค. ํ์ผ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์์ผ ํฉ๋๋ค.
MyXmlTransforms/
โโโ MyXmlTransforms.csproj
โโโ Program.cs๊ทธ ํ Program.cs ํ์ผ์ ์ญ์ ํ๊ณ Base๋ผ๋ ํด๋๋ฅผ ์์ฑํ ๋ค์ ๊ทธ ์์ dev.config.xml ํ์ผ์ ๋ฃ์ผ์ธ์. ๋ํ Prod๋ผ๋ ํด๋๋ฅผ ์์ฑํ๊ณ ๊ทธ ์์ prod.config.xml ํ์ผ์ ๋ฃ์ผ์ธ์. ๋ง์ง๋ง์ผ๋ก MyXmlTransforms.csproj ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์
๋ฐ์ดํธํ์ธ์.
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Transform">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<XdtDll>$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll</XdtDll>
<OutputDir>$(MSBuildThisFileDirectory)bin\Output\</OutputDir>
</PropertyGroup>
<UsingTask TaskName="TransformXml" AssemblyFile="$(XdtDll)" />
<ItemGroup>
<!-- ์ฌ๊ธฐ์ ํญ๋ชฉ์ ๋ ์ถ๊ฐํ์ธ์ -->
<XmlTransform Include="Config">
<Source>Base\dev.config.xml</Source>
<Transform>Prod\prod.config.xml</Transform>
<Destination>$(OutputDir)prod.config.xml</Destination>
</XmlTransform>
</ItemGroup>
<Target Name="Transform" BeforeTargets="Build">
<MakeDir Directories="$(OutputDir)" />
<TransformXml Source="%(XmlTransform.Source)" Transform="%(XmlTransform.Transform)" Destination="%(XmlTransform.Destination)" StackTrace="true" />
<Message Text="Transformed โ %(XmlTransform.Destination)" Importance="high" />
</Target>
</Project>
%(...)๋ฐฐ์น ๊ตฌ๋ฌธ์ MSBuild๊ฐ ํญ๋ชฉ๋น ํ ๋ฒ์ฉTransformXml์ ์๋์ผ๋ก ์คํํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ๋ฐ๋ผ์ ๋ฐ๋ณต๋ฌธ์ด ํ์ํ์ง ์์ต๋๋ค. ์์ ์StackTrace="true"์ต์ ์ ์ค์ ํ๋ฉด ๋ณํ์ด ์คํจํ ๊ฒฝ์ฐ ์์ธํ ์ค๋ฅ ์ถ๋ ฅ์ด ์ ๊ณต๋๋ฏ๋ก ์์ฑ ์ค์ ์ ์ฉํฉ๋๋ค.
๋ค์์ ํต์ฌ ์์ฝ์ ๋๋ค.
- Visual Studio์ ๋ด์ฅ๋
Microsoft.Web.Publishing.Tasks.dll์ ์ด์ฉํ XDT ๋ณํ dev๋ฐprod๊ตฌ์ฑ ํ์ผ์ ๋ณํฉํ๊ณbin/Output๋๋ ํฐ๋ฆฌ์ ์ถ๋ ฅํ๋ ๋ณํ ํ์ดํ๋ผ์ธ์ ์ ์ธํฉ๋๋ค.- ๋ณํํ ํ์ผ ์์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ,
XmlTransform์์๋ฅผ ๋ณต์ ํ๊ณ ํ์ผ์ ๋ฐ๋ผ ์์ฑ์ ์ฑ์ฐํด์ธ์.
๋ค์์ผ๋ก, ๋ค์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋ณํ์ ์คํํ์ธ์.
msbuild WithXdt.csproj /t:Transform๋๋ ๋ ๊ฐ๋จํ๊ฒ
dotnet buildํ์ผ ํธ๋ฆฌ๋ ์ด์ ๋ค์๊ณผ ๊ฐ์์ผ ํฉ๋๋ค.
MyXmlTransforms/
โโโ MyXmlTransforms.csproj
โโโ Base/
โ โโโ dev.config.xml
โโโ Prod/
โ โโโ prod.config.xml
โโโ bin/
โโโ Output/ <-- ๋น๋ ์์ ์ ์์ฑ๋จ์ผ๋ฐ์ ์ธ XDT ๋ณํ ์์ฑ
์ฌ๊ธฐ๋ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ณํ ์์ฑ์ด ์์ต๋๋ค.
| ๋๋ณธ | xdt:Transform | xdt:Locator |
|---|---|---|
| ์์ฑ์ ๋ณ๊ฒฝํ๋ค | SetAttributes(attrName) | Match(keyAttr) |
| ์ ์ฒด ์์๋ฅผ ๊ต์ฒดํ๋ค | Replace | Match(...) or XPath(...) |
| ์ ์์๋ฅผ ์ฝ์ ํ๋ค | Insert | (ํ์ ์์) |
| ์์๋ฅผ ์ญ์ ํ๋ค | Remove | Match(...) or XPath(...) |
| ์์ฑ์ ์ญ์ ํ๋ค | RemoveAttributes(attrName) | Match(...) |
์์ฝ
์ด ๊ฐ์ด๋์์๋ XDT ๋ณํ ์๋๋ฆฌ์ค, ๊ฐ๋จํ ๊ตฌ๋ฌธ ๋ฐ ํ๋ก์ ํธ ์ค์ ์ ๋ํด ์ค๋ช ํฉ๋๋ค. ์ฐธ๊ณ ์๋ฃ๋ก GitHub์ ์ ์ฌ ํ๋ก์ ํธ๋ฅผ ํ์ธํ์ธ์. ํด๋ ๊ธฐ๋ฐ ๋ณํ์ ๋ํด์๋ ์ด ํ๋ก์ ํธ๋ฅผ ์ฐธ์กฐํ์ธ์.